zishang520/socket.io

How to call join in the EventListenser?

Closed this issue · 2 comments

Hi,

I'm considering using this go lib to replace the old one.
Currently, in the code

// 'join' event handler,
// The 'join' workflow is as follows,
// 1. FE client got ack from server. Client side `connected` event is triggered.
// 2. In client connect event handler,
// socket.on('connect', () => {
// 	socket.emit('join', jwtToken);
// });
// 3. this event handler is triggered, and the connection is joined to a room called `email`
broadcastServer.OnEvent("", "join", func(s socketio.Conn, user string) {
	s.join(user)
})

How will this usage be supported by socket.io?

Thanks,

Hi, this might be the code snippet you need:

io.On("connection", func(args ...any) {
    socket := args[0].(*socket.Socket)

    utils.Log().Info(`socket %s connected`, socket.Id())

    // send an event to the client
    socket.Emit("foo", "bar")

    socket.On("foobar", func(...any) {
        // an event was received from the client
     })

     // join the room named "room1"
     socket.Join("room1")

     // broadcast to everyone in the room named "room1"
     io.To("room1").Emit("hello")

     // upon disconnection
     socket.On("disconnect", func(reason ...any) {
        utils.Log().Info(`socket %s disconnected due to %s`, socket.Id(), reason[0])
     })
})

Yea, so you can refer to the socket object in the event listener. Thanks!