猿问

我需要使用 go lang 连接到现有的 websocket 服务器

有一个 websocket 在我的本地主机上运行 ws://localhost:8080/ws

我需要可以创建 websocket 客户端并连接到该服务器的 go lang 代码。

我的 Google-Fu 技能没有教我一个简单的方法来做到这一点。

谢谢你。


慕村225694
浏览 215回答 2
2回答

繁花如伊

没关系,我在网上找到了一些帮助代码。现在我的代码看起来像这样,以防其他人需要这个:package mainimport (&nbsp; &nbsp; &nbsp; &nbsp; "net/http"&nbsp; &nbsp; &nbsp; &nbsp; "text/template"&nbsp; &nbsp; &nbsp; &nbsp; "code.google.com/p/go.net/websocket"&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "os"&nbsp; &nbsp; &nbsp; &nbsp; "time")const address string = "localhost:9999"func main() {&nbsp; &nbsp; initWebsocketClient()}func initWebsocketClient() {&nbsp; &nbsp; fmt.Println("Starting Client")&nbsp; &nbsp; ws, err := websocket.Dial(fmt.Sprintf("ws://%s/ws", address), "", fmt.Sprintf("http://%s/", address))&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Dial failed: %s\n", err.Error())&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; }&nbsp; &nbsp; incomingMessages := make(chan string)&nbsp; &nbsp; go readClientMessages(ws, incomingMessages)&nbsp; &nbsp; i := 0&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case <-time.After(time.Duration(2e9)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response := new(Message)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.RequestID = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.Command = "Eject the hot dog."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err = websocket.JSON.Send(ws, response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Send failed: %s\n", err.Error())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; case message := <-incomingMessages:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(`Message Received:`,message)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func readClientMessages(ws *websocket.Conn, incomingMessages chan string) {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; var message string&nbsp; &nbsp; &nbsp; &nbsp; // err := websocket.JSON.Receive(ws, &message)&nbsp; &nbsp; &nbsp; &nbsp; err := websocket.Message.Receive(ws, &message)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Error::: %s\n", err.Error())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; incomingMessages <- message&nbsp; &nbsp; }}同样正如recoba在评论中所建议的那样,这里有一个新的大猩猩示例,供寻求更好解决方案的人使用。

阿晨1998

看看这个基于事件的客户端,超级简单:https : //github.com/rgamba/evtwebsocket例子:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "github.com/rgamba/evtwebsocket"&nbsp; &nbsp; "golang.org/x/net/websocket")func main() {&nbsp; c := evtwebsocket.Conn{&nbsp; &nbsp; OnConnected: func(w *websocket.Conn) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Connected")&nbsp; &nbsp; },&nbsp; &nbsp; OnMessage: func(msg []byte) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Received message: %s\n", msg)&nbsp; &nbsp; },&nbsp; &nbsp; OnError: func(err error) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("** ERROR **\n%s\n", err.Error())&nbsp; &nbsp; },&nbsp; }&nbsp; // Connect&nbsp; c.Dial("ws://echo.websocket.org")&nbsp; c.Send([]byte("TEST"), nil)}
随时随地看视频慕课网APP

相关分类

Go
我要回答