Use with go-gin #34
Answered
by
zishang520
robertchar92
asked this question in
Q&A
-
Can i use this repo with go-gin? |
Beta Was this translation helpful? Give feedback.
Answered by
zishang520
Dec 13, 2023
Replies: 1 comment 1 reply
-
This is a short demo: package main
import (
"regexp"
"github.com/gin-gonic/gin"
_types "github.com/zishang520/engine.io-go-parser/types"
"github.com/zishang520/engine.io/v2/utils"
"github.com/zishang520/socket.io/v2/socket"
)
func main() {
r := gin.Default()
socketio := socket.NewServer(nil, nil)
socketio.Of(
regexp.MustCompile(`/\w+`),
nil,
).Use(func(client *socket.Socket, next func(*socket.ExtendedError)) {
utils.Log().Success("MId:%v", client.Connected())
next(nil)
}).On("connection", func(clients ...interface{}) {
client := clients[0].(*socket.Socket)
// utils.Log().Success("/ test Handshake:%v", client.Handshake())
client.Broadcast().Emit("hi test")
client.On("event", func(clients ...interface{}) {
// utils.Log().Success("/ test eventeventeventeventevent%v", clients)
})
client.On("disconnect", func(...interface{}) {
// utils.Log().Success("/ test disconnect")
})
client.On("chat message", func(msgs ...interface{}) {
// io.Of("/test", nil).Emit("hi", msgs...)
client.Emit("chat message", map[string]interface{}{
"message": _types.NewStringBufferString("xxx"),
})
})
})
r.GET("/socket.io/*any", gin.WrapH(socketio.ServeHandler(nil)))
r.POST("/socket.io/*any", gin.WrapH(socketio.ServeHandler(nil)))
r.Run(":3000") // listen and serve on 0.0.0.0:3000 (for windows "localhost:3000")
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
robertchar92
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a short demo: