I tried to use socket.io in gin,but I met a problem,I use it this way ` router.GET ( "/socket.io/",socketHandler)
router.POST ( "/socket.io/",socketHandler)
router.Handle ( "WS", "/socket.io/", socketHandler )
router.Handle ( "WSS", "/socket.io/",socketHandler )`
and socketHandler ` sio.On("/", func() {
log.Println("connect-----------")
})
sio.Of("/android").On("connect", func(ns *socketio.NameSpace) {
KeyMap[ns.Id()]=ns
log.Println("Pol Connected: ", ns.Id())
})
sio.Of("/android").On("disconnect", func(ns *socketio.NameSpace) {
delete(KeyMap, ns.Id())
log.Println("Pol Disconnected: ", ns.Id())
})
sio.ServeHTTP(c.Writer,c.Request)`
After page access, there is a socketHandler method,but No output,Then the console prints 404. The console has a lot of output `[GIN] 2018/07/16 - 00:16:38 | 404 | 0s | ::1 | GET /socket.io/?EIO=3&transport=polling&t=MIUkiRZ
`
Comment From: appleboy
See my example:
https://github.com/go-ggz/ggz/blob/master/router/router.go#L120-L124
Comment From: doquangtan
Use can use this lib socket.io golang. It is socketio server version 4.x and it can using with client version 3.x, 4.x
import (
"github.com/gin-gonic/gin"
socketio "github.com/doquangtan/socket.io/v4"
)
func main() {
io := socketio.New()
io.OnConnection(func(socket *socketio.Socket) {
// ...
})
router := gin.Default()
router.GET("/socket.io/", gin.WrapH(io.HttpHandler()))
router.Run(":3300")
}