kataras / go-websocket

:speaker: Deprecated. Use https://github.com/kataras/neffos instead
MIT License
60 stars 16 forks source link

How to receive javascript blob in websocket #25

Closed avinlakhera closed 7 years ago

avinlakhera commented 7 years ago

Hi,

I am creating a video streaming system in html5 and golang iris. I am using iris websocket with is very cool and easy to use. I am having one issue that how can we send the webcam blob to our iris websocket

below is the javascript code which reads the webcam data and sends the our websockets mediaRecorder.ondataavailable = function (e) { console.log("binarystream") console.log(e.data); socket.Emit("binarystream", e.data); }

Below is the Golang code

serv.Websocket.OnConnection(func(socket iris.WebsocketConnection) {

    socket.On("binarystream", func(m interface{}) {
            f := m.(map[string]interface{})
                         var buf bytes.Buffer
                        enc := gob.NewEncoder(&buf)
                        err := enc.Encode(f)
    })

})

I have also set the Binary Message configuration to true.

But I think I am not receving the exact data which is send from the browser . I am missing some configuration here . Can you let me know about it if I required some extra configuration .

Thanks

ghost commented 7 years ago

Hello @avinlakhera,

Thanks for your nice words, hope you enjoy the framework.

If you set the BinaryMessage to true then it waits for raw websocket []byte messages( this is the best option for your case and if you work with proto3). You should use the OnMessage(func(b []byte)) instead of On("customevent", func(v interface{}) and EmitMessage([]byte) to send raw []byte to the client-side instead of Emit("customevent", data) .

Note: If this library doesn't suits your needs you can always use any third-party websocket servers with Iris, like socket.io.

Thanks, kataras

avinlakhera commented 7 years ago

so can I use socket.io iris plugins ?

ghost commented 7 years ago

Your application can be structured with the kataras/go-websocket too but if you want to use socket.io you can do it.

There is not a plugin, it's just a golang library which works with net/http. Iris is 100% compatible with net/http so you can use it with Iris too. Just use the iris.ToHandler helper function to convert a net/http.Handler/HandlerFunc to an iris.Handler and you should be fine.

Example:

package main

import (
     "log"

    "github.com/kataras/iris"
    "github.com/googollee/go-socket.io"
)

func main() {
    app := iris.New()

    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.On("connection", func(so socketio.Socket) {
        log.Println("on connection")
        so.Join("chat")
        so.On("chat message", func(msg string) {
            log.Println("emit:", so.Emit("chat message", msg))
            so.BroadcastTo("chat", "chat message", msg)
        })
        so.On("disconnection", func() {
            log.Println("on disconnect")
        })
    })
    server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
    })

    app.Any("/socket.io", iris.ToHandler(server))

    app.Listen(":5000")
}
avinlakhera commented 7 years ago

Thanks for solution. I will try both the ways you have suggested.

ghost commented 7 years ago

You're welcome, if you need further help don't hesitate to ask again!

zkcrescent commented 7 years ago

Hi, the app.Any("/socket.io", iris.ToHandler(server)) is not used now and app.Any("/socket.io", iris.FromStd(server)) seems useless either. So , could you tell me how to fix this. THX