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

Upgrade context? #5

Closed
vinhjaxt opened this issue Apr 5, 2020 · 6 comments
Closed

Upgrade context? #5

vinhjaxt opened this issue Apr 5, 2020 · 6 comments

Comments

@vinhjaxt
Copy link

vinhjaxt commented Apr 5, 2020

Hi,
First of all, thank you for great repo.
In my case, I want to pass some data to the ws handler like this:

var wsUpgrader = &fastws.Upgrader{
	Compress: true,
	Handler: func(c *fastws.Conn, yourCtx interface{}) {
		var (
			bf  []byte
			err error
		)
		myCtx := yourCtx.(*MyContext)

		c.ReadTimeout = 0
		c.WriteTimeout = 0

		for {
			_, bf, err = c.ReadMessage(bf[:0])
			if err != nil {
				/*
					if err != fastws.EOF {
						log.Println("error reading message:", err)
					}
					// */
				break
			}
			log.Println("UID send data:", myCtx.UID, string(bf))
		}
	},
	UpgradeHandler: func(ctx *fasthttp.RequestCtx, yourCtx interface{}) bool {
		myCtx := yourCtx.(*MyContext)
		myCtx.UID = "456"
		return true
	},
}

func requestHandler(ctx *fasthttp.RequestCtx) {
	myCtx := &MyContext{
		UID: "213",
	}
	wsUpgrader.Upgrade(ctx, myCtx)
}
@dgrr
Copy link
Owner

dgrr commented Apr 5, 2020

That's interesting. I can use something like fasthttp does with ctx.UserValue widely used by routers and other middlewares. Would you like me to add that?
I can't change the Handler method but I can add the UserValues to conn. Also, compression is not working yet, I will try to work on that this week.

@vinhjaxt
Copy link
Author

vinhjaxt commented Apr 5, 2020

Thank you for the quick response.
I think adding UserValues ​​to Conn will do the trick.

@dgrr
Copy link
Owner

dgrr commented Apr 5, 2020

Ok. I will add that for now. I will try to do other workaround later on

@dgrr dgrr closed this as completed Apr 5, 2020
dgrr added a commit that referenced this issue Apr 5, 2020
@dgrr
Copy link
Owner

dgrr commented Apr 5, 2020

You can do the following:

upgr := Upgrader{
	Origin: uri,
	Handler: func(conn *Conn) {
		v := conn.UserValue("custom")
		if v == nil {
			t.Fatal("custom is nil")
		}
		conn.WriteString(v.(string))
	},
}
s := fasthttp.Server{
	Handler: func(ctx *fasthttp.RequestCtx) {
		ctx.SetUserValue("custom", text)
		upgr.Upgrade(ctx)
	},
}

All the values will be copied from the fasthttp's RequestCtx

@dgrr
Copy link
Owner

dgrr commented Apr 5, 2020

https://github.com/dgrr/fastws/blob/master/examples/pass_value.go

@vinhjaxt
Copy link
Author

vinhjaxt commented Apr 5, 2020

Good work!!
Thank you!!

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

No branches or pull requests

2 participants