How to setup / run the socket.io compatible server ? https://github.com/googollee/go-socket.io/

Tried 2 methods

server, err := socketio.NewServer(nil)
1) server.ServeHTTP ( c.Writer, c.Request )
2) http.Handle("/socket.io/", server)

But can't connect from the client, loops in

 POST /socket.io/
  GET /socket.io/
 POST /socket.io/
  GET /socket.io/

Comment From: Thomasdezeeuw

2 won't work because it has it's own context, so it should error out when building. I think you need something like this:

Server.Post(func (ctx *gin.Context) { SocketServer(ctx.Writer, ctx.Request) })

On a phone, so it's not tested.

Comment From: sjbog

1st soltion seems to be the correct approach

Task is to make socket.io library's example to work.

Requests flow : 1) GET /socket.io/?EIO=3&transport=polling&t=1411131107316-0 200 OK

�0{"sid":"fwewxAtun6tLzrXuukUy","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}

2) GET /socket.io/?EIO=3&transport=polling&t=1411131107514-1&sid=fwewxAtun6tLzrXuukUy 400 Bad Request invalid sid

2`) ws://127.0.0.1:8000/socket.io/?EIO=3&transport=websocket&sid=fwewxAtun6tLzrXuukUy WebSocket is closed before the connection is established.

Backend log :

backend_log

POST requests are the same as 2) invalid sid

ps: It seems like something is prepending extra symbols to JSON. But in firefox 1) response is "application/octet-stream" with content

AAkI/zB7InNpZCI6IlRNeFRWQnVMeS00ZWFqeWczNTBzIiwidXBncmFkZXMiOlsid2Vic29ja2V0Il0sInBpbmdJbnRlcnZhbCI6MjUwMDAsInBpbmdUaW1lb3V0Ijo2MDAwMH0K

while in chrome it is "text/plain"

Comment From: manucorporat

is this a bug related with Gin? can you provide the whole source code? the socket.io library's example.

Comment From: sjbog

Found the issue, unlike go.net/websocket lib, here websocket server should run in parallel with gin server.

var Socketio_Server * socketio.Server

func main() {
    var router_engine = gin.Default ()
    var err error

    Socketio_Server, err    = socketio.NewServer(nil)
    if  err != nil  {
        panic ( err )
    }

    router_engine.GET ( "/", IndexHandler )
    router_engine.Static ( "/public", "./public" )

    router_engine.GET ( "/socket.io", socketHandler )
    router_engine.POST ( "/socket.io", socketHandler )
    router_engine.Handle ( "WS", "/socket.io", [] gin.HandlerFunc { socketHandler } )
    router_engine.Handle ( "WSS", "/socket.io", [] gin.HandlerFunc { socketHandler } )

    router_engine.Run(":8000")
}

func socketHandler ( c  * gin.Context ) {
    Socketio_Server.On("connection", func(so socketio.Socket) {
        fmt.Println("on connection")

        so.Join("chat")

        so.On("chat message", func(msg string) {
            fmt.Println("emit:", so.Emit("chat message", msg))
            so.BroadcastTo("chat", "chat message", msg)
        })
        so.On("disconnection", func() {
            fmt.Println("on disconnect")
        })
    })

    Socketio_Server.On ( "error", func( so socketio.Socket, err error) {
        fmt.Printf ( "[ WebSocket ] Error : %v", err.Error () )
    })

    Socketio_Server.ServeHTTP ( c.Writer, c.Request )
}

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")
}