socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

How to get the server room

Open JanYork opened this issue 1 year ago • 6 comments

How can I get all the rooms of a socket.Server and the socket.Client information in the room like socket.io node sdk?

JanYork avatar Jun 30 '24 11:06 JanYork

Hello, you can refer to the following demo. Of course, you can directly check the Node.js Socket.IO documentation. The Go version of Socket.IO has an SDK that is basically consistent with the Node.js version of Socket.IO.

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/zishang520/engine.io/v2/log"
	"github.com/zishang520/engine.io/v2/types"
	"github.com/zishang520/engine.io/v2/utils"
	"github.com/zishang520/socket.io/v2/socket"
)

func main() {
	log.DEBUG = true
	c := socket.DefaultServerOptions()
	c.SetAllowEIO3(true)
	// c.SetConnectionStateRecovery(&socket.ConnectionStateRecovery{})
	// c.SetAllowEIO3(true)
	// c.SetPingInterval(300 * time.Millisecond)
	// c.SetPingTimeout(200 * time.Millisecond)
	c.SetMaxHttpBufferSize(1000000)
	c.SetConnectTimeout(1000 * time.Millisecond)
	c.SetCors(&types.Cors{
		Origin:      "*",
		Credentials: true,
	})
	httpServer := types.NewWebServer(nil)
	io := socket.NewServer(httpServer, c)
	io.On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.Emit("auth", client.Handshake().Auth)

		client.On("message", func(args ...interface{}) {
			client.Emit("message-back", args...)
		})

		client.On("message-with-ack", func(args ...interface{}) {
			ack := args[len(args)-1].(func([]any, error))
			ack(args[:len(args)-1], nil)
		})
	})

	io.Of("/custom", nil).On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.Emit("auth", client.Handshake().Auth)
	})

	utils.SetTimeout(func() {
		// main namespace
		root_rooms := io.Of("/", nil).Adapter().Rooms()
		root_sids := io.Of("/", nil).Adapter().Sids()
		fmt.Println(root_rooms)
		fmt.Println(root_sids)

		// custom namespace
		rooms := io.Of("/custom", nil).Adapter().Rooms()
		sids := io.Of("/custom", nil).Adapter().Sids()
		fmt.Println(rooms)
		fmt.Println(sids)
		// return all Socket instances in the "room1" room
		io.FetchSockets()(func(sockets []*socket.RemoteSocket, _ error) {

			for _, socket := range sockets {
				fmt.Println(socket.Id())
				fmt.Println(socket.Handshake())
				fmt.Println(socket.Rooms())
				fmt.Println(socket.Data())

				// socket.Emit("hello")
				// socket.Join("room1")
				// socket.Leave("room2")
				// socket.Disconnect()
			}

		})
	}, time.Millisecond*5000)

	httpServer.Listen(":3000", nil)

	exit := make(chan struct{})
	SignalC := make(chan os.Signal)

	signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range SignalC {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				close(exit)
				return
			}
		}
	}()

	<-exit
	io.Close(nil)
	os.Exit(0)
}

zishang520 avatar Jul 03 '24 02:07 zishang520

Do you have any other questions? If there are no more, the issue will be closed.

zishang520 avatar Jul 11 '24 06:07 zishang520

Do you have any other questions? If there are no more, the issue will be closed.

How to get the reason parameter when the connection is disconnected? Like node to socket.io:

io.on('disconnect', async (reason) => {
    //....
});

There is no documentation on what parameters events.Listener will return and in what order.

JanYork avatar Jul 17 '24 06:07 JanYork

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

zishang520 avatar Jul 17 '24 09:07 zishang520

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

Thanks, And, How can I get all socket instances in a room of a socket server?

JanYork avatar Aug 01 '24 05:08 JanYork

Did you read the example code in https://github.com/zishang520/socket.io/issues/59#issuecomment-2204908394 carefully?

zishang520 avatar Aug 02 '24 02:08 zishang520

Do you have any other questions? If there are no more, the issue will be closed.

zishang520 avatar Sep 22 '24 13:09 zishang520