Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤗 [Question]: Lost MAP data #3195

Closed
3 tasks done
RhaPT opened this issue Nov 10, 2024 · 5 comments
Closed
3 tasks done

🤗 [Question]: Lost MAP data #3195

RhaPT opened this issue Nov 10, 2024 · 5 comments

Comments

@RhaPT
Copy link

RhaPT commented Nov 10, 2024

Question Description

Sorry about my English.
I created a page with javascript where it creates a websocket and receives a unique id ("Sec-Websocket-Key") for that instance, then it uses this id whenever it makes a request to the api (sends in the header an X-Id with this value), the api stores data within a map indexed by X-Id

If you open another window in the same browser (if use other browser no problem) and call the same page, it will create a new instance for in websocket and at that time it automatically changes the MAP.

Example the first time you open the page: map[]
after adding a value : map[GlYkSNQ6GQMFtdkHGqlxZw==:GlYkSNQ6GQMFtdkHGqlxZw==]
after open new window in same browser: map[keep-aliveMFtdkHGqlxZw==:keep-aliveMFtdkHGqlxZw==]

Does anyone know why the Map changes?

Code Snippet (optional)

package main

import (
	"log"
	"github.com/gofiber/contrib/websocket"
	"github.com/gofiber/fiber/v2"
)

var Xpto map[string]string

func init() {
	Xpto = make(map[string]string)
}

func main() {
	app := fiber.New()

	app.Static("/", ".")

	api := app.Group("/api")
	api.Post("/add", setXpto)

	app.Use("/ws", func(c *fiber.Ctx) error {
		if websocket.IsWebSocketUpgrade(c) {
			return c.Next()
		}
		return fiber.ErrUpgradeRequired
	})
	app.Get("/ws", websocket.New(func(c *websocket.Conn) {
		var (
			mt  int
			msg []byte
			err error
		)
		for {
			if mt, msg, err = c.ReadMessage(); err != nil {
				log.Println("read:", err)
				break
			}
			if string(msg) == "1" {
				newID := c.Headers("Sec-Websocket-Key", "")
				c.WriteMessage(mt, []byte(newID))
			}
			log.Println(Xpto)
		}
	}))
	log.Fatal(app.Listen(":3000"))

	log.Panicln(Xpto)
}

func setXpto(c *fiber.Ctx) error {
	xID := c.Get("X-Id", "")
	Xpto[xID] = xID
	log.Println(Xpto)
	c.Status(fiber.StatusOK)
	return c.JSON(Xpto)
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Nov 10, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@gaby
Copy link
Member

gaby commented Nov 11, 2024

@RhaPT First you never call init(). Second, the values from the context are only valid during the handler execution.

Here:

func setXpto(c *fiber.Ctx) error {
	xID := c.Get("X-Id", "")
	Xpto[xID] = xID
	log.Println(Xpto)
	c.Status(fiber.StatusOK)
	return c.JSON(Xpto)
}

You have to copy the value form the context like this:

xID := utils.CopyString(c.Get("X-Id", ""))

Documentation is here: https://docs.gofiber.io/#zero-allocation

@gaby gaby added the v2 label Nov 11, 2024
@RhaPT RhaPT closed this as completed Nov 11, 2024
@gaby
Copy link
Member

gaby commented Nov 11, 2024

@RhaPT Did that fix your issue?

@RhaPT
Copy link
Author

RhaPT commented Nov 11, 2024

Thanks for your help

@gaby
Copy link
Member

gaby commented Nov 11, 2024

Awesome 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants