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
How to pass values to websocket handler in iris websocket #1090
Comments
func handleWSconnection(s myStruct) func(websocket.Connection) {
return func(c websocket.Connection) {
// ...use of "s" + websocket conn here
}
)
ws.OnConnection(handleWSconnection(myStructInstance)) |
Thank you so much. |
You are very welcomed! |
@kataras, Unfortunately there seems to be an issue with this setup. The Websocket handler gets triggered multiple times when additional users trigger the websocket handler. When there is only one user, the websocket gets triggered once. When a second user triggers the same websocket, it gets triggered twice for the second user, 3 times for the 3rd user and so on. How do I make the handler trigger only once ? func handleWSconnection(s *mySession) func(websocket.Connection) {
return func(c websocket.Connection) {
c.On("tick", func(str string) {
// This gets triggered MULTIPLE TIMES
}
}
)
func main(){
//........
//........
ws := websocket.New(websocket.Config{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
EnableCompression: true,
})
app.Get("/", func(ctx iris.Context) {
goSession := sess.Start(ctx)
userId, err := goSession.GetInt("userId")
currentSession := &userSession {
uid: userId,
}
ws.OnConnection(handleWSconnection(currentSession))
}
app.Any("/ws", ws.Handler())
app.Any("/iris-ws.js", func(ctx iris.Context) {
ctx.Write(websocket.ClientSource)
})
} |
@kickbox You have two ways to deal context access inside websocket handler or a websocket connection inside a context (the same thing but opossite logical-flow), read below: If you need access to the func handleWSconnection(c websocket.Connection) {
ctx := c.Context()
goSession := sess.Start(ctx)
userId, err := goSession.GetInt("userId")
currentSession := &userSession{
uid: userId,
}
//
// register your functions to that conn.
//
c.On("tick", func(str string) {
// use the `currentSession` here.
})
}
func main(){
//........
//........
ws := websocket.New(websocket.Config{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
EnableCompression: true,
})
// register the listener once, so each connection goes through that
// handleWSconnection
ws.OnConnection(handleWSconnection)
app.Any("/ws", ws.Handler()) // use /ws or /, you know.
app.Any("/iris-ws.js", func(ctx iris.Context) { // or ("/ iris-ws.js", ws.ClientHandler())
ctx.Write(websocket.ClientSource)
})
} Secondly, If you want to upgrade the http to websocket inside an HTTP handler you can do it by using the ws.Upgrade(ctx) function and remove the package main
func handleWSconnectionWithSession(c websocket.Connection, s *mySession) {
c.On("tick", func(str string) {
// [...]
})
}
func main() {
//........
//........
ws := websocket.New(websocket.Config{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
EnableCompression: true,
})
app.Any("/ws", func(ctx iris.Context) {
goSession := sess.Start(ctx)
userId, err := goSession.GetInt("userId")
currentSession := &userSession{
uid: userId,
}
c := ws.Upgrade(ctx)
if c.Err() != nil {
return
}
//
// start the websocket work here
//
handleWSconnectionWithSession(c, currentSession)
//
// end your custom websocket work here.
//
// call `Wait` in order to block the handler
// and to 'activate' the connection's events.
c.Wait()
})
app.Any("/iris-ws.js", func(ctx iris.Context) { // or ("/ iris-ws.js", ws.ClientHandler())
ctx.Write(websocket.ClientSource)
})
} |
Thank you so much @kataras. I really appreciate your examples and thank you for taking the time to write two more example here on WebSocket. The ease of use and the abundance of examples in iris is what led me to use your package. Thanks again. I will surely look at examples in more detail. |
Thank you for the amazing websocket plugin. Its very simple to use.
Since I would like to avoid global variables, I want to pass a custom struct to the websocket handler. I tried overloading the handler function, but I get "used as value" error.
Here is the calling function and handler
This is the error in the command line
Please suggest how to handle this scenario. Thanks in advance.
The text was updated successfully, but these errors were encountered: