Replies: 2 comments
-
package main
import (
"github.com/lxzan/gws"
)
func GetValueFromSession[T any](s gws.SessionStorage, k string) (value T, ok bool) {
if v, exist := s.Load(k); exist {
value, ok = v.(T)
}
return
}
type Handler struct {
gws.BuiltinEventHandler
conns *gws.ConcurrentMap[int64, *gws.Conn]
}
func (c *Handler) OnOpen(socket *gws.Conn) {
userId, _ := GetValueFromSession[int64](socket.Session(), "userId")
c.conns.Store(userId, socket)
}
func (c *Handler) OnClose(socket *gws.Conn, err error) {
userId, _ := GetValueFromSession[int64](socket.Session(), "userId")
c.conns.Delete(userId)
}
func (c *Handler) Broadcast(p []byte) {
var b = gws.NewBroadcaster(gws.OpcodeText, p)
defer b.Close()
c.conns.Range(func(userId int64, socket *gws.Conn) bool {
_ = b.Broadcast(socket)
return true
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
lxzan
-
Hello, Thank you a lot for your valued assistance, i will check now how to pass an userId from the client webpage with socket.io and get it in the badckend. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
Thanks for this library.
I try to use Broadcast on the Best Practice example.
As it works per socket connection, how do you implement the []gws.Conn array to broadcast to it and manage the users disconnection ?
I saw the sessions storage section but i didn't find a way to use it properly.
Thanks in advance for your help :)
Arnaud.
Beta Was this translation helpful? Give feedback.
All reactions