1 回答

TA貢獻1818條經驗 獲得超7個贊
您可以將Websocket視為服務器和客戶端之間的直接信息管道 - 并且像Unix管道一樣,信息可以從兩端發送和接收。
gorilla/websocket正是以這種方式工作。您需要從第29-50行查看此處,了解如何連接到websocket服務器并讀取從服務器端發送的消息。簡而言之,要發送消息:
// c *websocket.Conn needs to be initialized from websocket.DefaultDialer.Dial
err := 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 |
-+--------+-------------------------------------+-----------|
- 1 回答
- 0 關注
- 105 瀏覽
添加回答
舉報