doushi8231
2019-01-01 10:21
浏览 195

如何使用socket.io连接Golang和Android?

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?

图片转代码服务由CSDN问答提供 功能建议

我想使用socket.io将数据从Android发送到Golang。 我使用Node.js正确地做到了,但是现在, 我想使用Go。 我找不到简单的示例。我该怎么办?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanruinong0619 2019-01-02 04:53
    已采纳

    I assume you would like to use a Go implementation of a 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))
    }
    
    打赏 评论

相关推荐 更多相似问题