gofiber / contrib

🧬 Repository for third party middlewares with dependencies
https://docs.gofiber.io/contrib/
MIT License
218 stars 115 forks source link

🤗 [Question]: How does socketio differentiate between TextMessage and BinaryMessage? #1114

Closed liaohongxing closed 4 months ago

liaohongxing commented 4 months ago

Question Description

@antoniodipinto

Code Snippet (optional)

socketio.On(socketio.EventMessage, func(ep *socketio.EventPayload) {
    fmt.Println("==EventMessage==", string(ep.Data))

    if (TextMessage) {
        //TextMessage
    }
    if (BinaryMessage) {
        //BinaryMessage
    }
})

Checklist:

antoniodipinto commented 4 months ago

Hi @liaohongxing here's how socketio manages this. By default websocket protocol defines the datatype. More information about this can be found here https://www.rfc-editor.org/rfc/rfc6455.html#section-11.8

Code reference Gorilla WebSocket https://github.com/gorilla/websocket/blob/main/conn.go#L61 Code reference for socketio fiber https://github.com/gofiber/contrib/blob/main/socketio/socketio.go#L16

Needs to be mentioned that this implementation supports read/write only text/byte data, like JSON, strings and every type that can be converted to []byte/string/text

liaohongxing commented 4 months ago

Thanks for your quick response, I read your tip carefully.

The EventPayload callback seems to lack a flag to distinguish the two message types

For example, I want to receive messages and files in the callback, how can I do it?

antoniodipinto commented 4 months ago

Hi @liaohongxing as I mentioned, there is no need to check the message type, because this implementation supports only WebsocketMessageType = 1 (Text Message). The solution for your problem can be pretty simple using Base64 to encode the file, depending on your implementation.

liaohongxing commented 4 months ago

Well. I think I know how to do it. thank you