Replies: 7 comments
|
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)
} |
|
Do you have any other questions? If there are no more, the issue will be closed. |
How to get the io.on('disconnect', async (reason) => {
//....
});There is no documentation on what parameters events.Listener will return and in what order. |
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? |
|
Did you read the example code in #84 (comment) carefully? |
|
Do you have any other questions? If there are no more, the issue will be closed. |
Uh oh!
There was an error while loading. Please reload this page.
How can I get all the
roomsof asocket.Serverand thesocket.Clientinformation in theroomlikesocket.io node sdk?All reactions