How do I connect to a 3rd party ws service in golang as a client with gorilla in golang? Or is it not possible?

huangapple go评论75阅读模式
英文:

How do I connect to a 3rd party ws service in golang as a client with gorilla in golang? Or is it not possible?

问题

我只能为您提供翻译服务,以下是您要翻译的内容:

我只想使用Gorilla WebSocket包连接到一个交易WebSocket地址,但我只找到了WebSocket实现的服务器端代码。我该如何连接到WebSocket地址并发送/接收消息?是否有代码示例?

英文:

I just want to connect to a trading ws address with gorilla websocket package but all I could find was the server side of the web socket implementation. How do I connect to a ws address and send/receive messages from it. Is there an code example?

答案1

得分: 1

Websocket可以被视为服务器和客户端之间的直接信息传输管道,就像Unix管道一样,信息可以从两端发送和接收。

gorilla/websocket正是以这种方式工作。你可以在这里的29-50行查看如何连接到Websocket服务器并读取来自服务器端发送的消息。简而言之,要发送一条消息:

// 需要使用websocket.DefaultDialer.Dial初始化c *websocket.Conn

err := c.WriteMessage(websocket.TextMessage, []byte("Hello, World!"))

要读取一条消息:

messageType, msg, err := c.ReadMessage()

你可能不需要或关心从调用c.ReadMessage()返回的messageType,但以防万一你需要,它在Websocket RFC规范中定义:

     |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  |
    -+--------+-------------------------------------+-----------|
英文:

You can think of a Websocket as a direct pipeline of information between a server and a client - and like Unix pipes, information can be both sent and received from both ends.

gorilla/websocket works exactly this way. You'll want to take a look here from lines 29-50 on how to connect to a websocket server and read for messages sent from the server end. In short, to send a message:

// c *websocket.Conn needs to be initialized from websocket.DefaultDialer.Dial

err := c.WriteMessage(websocket.TextMessage, []byte("Hello, World!"))

And to read a message:

messageType, msg, err := c.ReadMessage()

You probably won't need or care about the messageType returned from a call to c.ReadMessage(), but just in case you do, it's defined in the Websocket RFC Spec:

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

huangapple
  • 本文由 发表于 2021年8月17日 23:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/68820151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定