I'm pretty new in Go! but I just finished to migrate my nodejs REST api to Go... I need to implement a behaviour of sails.js framwork which can handle socket.io requests and simulate http ones...the only way to do it with Gin I found was 'mocking' the request like I do on my test:
return func(so *gosocketio.Channel, req SockRequest) SockResponse {
// Prepare the request (SockRequest is a json incoming on the socket unmarshaled by socket.io)
request, _ := http.NewRequest(http.MethodGet, req.Url, nil)
for prop, value := range req.Headers {
request.Header.Set(prop, value)
}
request.Header.Set("SockId", so.Id())
writer := httptest.NewRecorder()
srv.ServeHTTP(writer, request)
var body interface{}
_ = json.Unmarshal(writer.Body.Bytes(), &body)
return SockResponse{Body: body, Headers: writer.Header(), StatusCode: writer.Code}
}
I have a huge drop of performance, I assume is on the chain here, as a lots of marshaling, unmarshaling, mocking and the httptest.NewRecorder()...
So my question is, any advice? what's the best way to do this? Thanks in advance!
Comment From: cp-sumi-k
If you are still pending with this issue, can use this awesome socket.io library written in golang.
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")
}