亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 Golang 和 Android 與 socket.io 連接起來?

如何將 Golang 和 Android 與 socket.io 連接起來?

Go
Cats萌萌 2023-05-22 17:40:24
我想使用 socket.io 將數據從 Android 發送到 Golang。我用 Nodejs 正確地做到了但是現在,我想用 Go 來做。我找不到簡單的例子。我該怎么做?
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

我假設您想使用 Socket.IO 服務器庫的 Go 實現而不是標準的 Node.js 實現。如果是這樣,您可以嘗試googollee/go-socket.io 項目。這是項目頁面中的示例:

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))

}


查看完整回答
反對 回復 2023-05-22
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號