androidgowebsocketsocket.iogolang-migrate

How can connect Golang and Android with socket.io ?


I want to send data from Android to Golang with socket.io . I do that with Nodejs correctly But now , i want do with Go. I cant find simple example.how can i do that?


Solution

  • I assume you would like to use a Go implementation of the Socket.IO server library instead of the standard Node.js one. If so, you can try googollee/go-socket.io project. Here is an example from the project page:

    package main
    
    import (
        "log"
        "net/http"
    
        "github.com/googollee/go-socket.io"
    )
    
    func main() {
        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))
                server.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)
        })
    
        http.Handle("/socket.io/", server)
        http.Handle("/", http.FileServer(http.Dir("./asset")))
        log.Println("Serving at localhost:5000...")
        log.Fatal(http.ListenAndServe(":5000", nil))
    }