猿问

如何连接到戈兰的第三方ws服务,作为客户与戈兰的大猩猩?还是不可能?

我只想连接到一个交易ws地址与大猩猩websocket包,但我能找到的只是Web套接字实现的服务器端。如何连接到 ws 地址并从中发送/接收消息。是否有代码示例?


侃侃无极
浏览 148回答 1
1回答

qq_笑_17

您可以将Websocket视为服务器和客户端之间的直接信息管道 - 并且像Unix管道一样,信息可以从两端发送和接收。gorilla/websocket正是以这种方式工作。您需要从第29-50行查看此处,了解如何连接到websocket服务器并读取从服务器端发送的消息。简而言之,要发送消息:// c *websocket.Conn needs to be initialized from websocket.DefaultDialer.Dialerr := c.WriteMessage(websocket.TextMessage, []byte("Hello, World!"))并阅读一条消息:messageType, msg, err := c.ReadMessage()您可能不需要或不关心从调用返回的 ,但为了以防万一,它在 Websocket RFC 规范中定义:messageTypec.ReadMessage()     |Opcode  | Meaning                             | Reference |    -+--------+-------------------------------------+-----------|     | 0      | Continuation Frame                  | RFC 6455  |    -+--------+-------------------------------------+-----------|     | 1      | Text Frame                          | RFC 6455  |    -+--------+-------------------------------------+-----------|     | 2      | Binary Frame                        | RFC 6455  |    -+--------+-------------------------------------+-----------|     | 8      | Connection Close Frame              | RFC 6455  |    -+--------+-------------------------------------+-----------|     | 9      | Ping Frame                          | RFC 6455  |    -+--------+-------------------------------------+-----------|     | 10     | Pong Frame                          | RFC 6455  |    -+--------+-------------------------------------+-----------|
随时随地看视频慕课网APP

相关分类

Go
我要回答