如何在bash中阅读websocket响应

J. *_*Doe 21 bash curl websocket

编写一个bash脚本以连接到gss:http://ws-feed.gdax.com 上的GDAX的Websocket Feed,但curl似乎不支持这个

curl "wss://ws-feed.gdax.com"
curl: (1) Protocol "wss" not supported or disabled in libcurl
Run Code Online (Sandbox Code Playgroud)

Tra*_*rke 26

假设你已node安装,我会wscat试一试; 它简单,直观,功能强大.否则,@ Pavel的答案有很多令人尊敬的websocket客户端替代方案.

# install
npm install -g wscat

# use
wscat -c "wss://ws-feed.gdax.com"
Run Code Online (Sandbox Code Playgroud)

  • 在 Ubuntu Bionic 上,它只能通过“sudo apt install node-ws”运行 (4认同)
  • 在基于 Debian 的发行版中,您可以“sudo apt install node-ws”。 (4认同)
  • @Rambatino - WebSocket 协议 [规范](https://tools.ietf.org/html/rfc6455) 定义了 `ws` (WebSocket) 和 `wss` (WebSocket Secure)。 (2认同)

Pav*_*vel 18

好吧,您可以尝试使用curl模拟所需的标头以获得一些响应:

此外,还有其他与WebSocket服务器通信的方式,例如


igo*_*ack 12

ws通过协议启动连接http,您必须更改wshttp一些额外的标头,如下所示:

curl --include \
     --no-buffer \
     --header "Connection: Upgrade" \
     --header "Upgrade: websocket" \
     --header "Host: example.com:80" \
     --header "Origin: http://example.com:80" \
     --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
     --header "Sec-WebSocket-Version: 13" \
     "https://ws-feed.gdax.com"
Run Code Online (Sandbox Code Playgroud)

https://gist.github.com/htp/fbce19069187ec1cc486b594104f01d0


Vi.*_*Vi. 9

我想为此添加自己的工具:websocat

有关服务的示例会话:

$ rlwrap  websocat wss://ws-feed.gdax.com

# Now enter this line (without the #) for the required JSON request:
# {"type":"subscribe","channels": [{ "name": "heartbeat", "product_ids": ["BTC-USD"] }]}

{"type":"subscriptions","channels":[{"name":"heartbeat","product_ids":["BTC-USD"]}]}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079752,"time":"2018-07-12T22:32:42.655000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079800,"time":"2018-07-12T22:32:43.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079834,"time":"2018-07-12T22:32:44.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079945,"time":"2018-07-12T22:32:45.656000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312079990,"time":"2018-07-12T22:32:46.657000Z"}
{"type":"heartbeat","last_trade_id":46274575,"product_id":"BTC-USD","sequence":6312080042,"time":"2018-07-12T22:32:47.657000Z"}
{"type":"heartbeat","last_trade_id":46274576,"product_id":"BTC-USD","sequence":6312080169,"time":"2018-07-12T22:32:48.657000Z"}

# To stop the feed, type this line: 
{"type":"unsubscribe","channels": [{ "name": "heartbeat", "product_ids": ["BTC-USD"] }]}
{"type":"subscriptions","channels":[]}
Run Code Online (Sandbox Code Playgroud)

除了websocket客户端之外,websocat还支持WebSocket服务器和其他模式,并且通常旨在将websocket集成到“ UNIX”世界中。

  • “websocat”让我立即运行,甚至在 MacOS 上也是如此!`brew install websocat` 然后只需在命令行上提供套接字服务器地址即可进行对话!惊人的! (2认同)

x-y*_*uri 7

考虑到这里有curl答案,虽然curl你可以建立连接但无法发送帧,我会添加我的。

我看到的答案curl是他们给了你一些命令,但没有解释。这就是我想填补的空白。

简而言之,websocket 协议是如何工作的?客户端连接到服务器,发送握手请求,接收“101 交换协议”(握手响应),然后来回发送帧。

握手看起来如下:

GET / HTTP/1.1
Host: ws.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Version: 13
Origin: http://example.com

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Run Code Online (Sandbox Code Playgroud)

Upgrade使其从 HTTP(s) 切换到 websocket 协议。

Connection指定这Upgrade是一个逐跳标头(中介应该使用而不是转发的标头)。但我的实验表明它无需此标头即可工作。或者更准确地说,如果服务器前面有反向代理(例如nginx),则它可能是可选的。

Sec-WebSocket-Key/是此处此处此处Sec-WebSocket-Accept描述的安全措施。

Sec-WebSocket-Version指定 websocket 协议版本。根据 RFC 6455,它应该等于13。

Origin当客户端是浏览器并且请求页面的来源与 websocket 服务器 URL 的来源不匹配时需要。

有关构成 websocket 握手请求的详细说明,请参见此处

HTTP/1.1 就是这样。HTTP/2 则是另一回事

了解这一点可以与以下内容建立 websocket 连接curl

$ curl -H 'Upgrade: websocket' \
       -H "Sec-WebSocket-Key: `openssl rand -base64 16`" \
       -H 'Sec-WebSocket-Version: 13' \
       --http1.1 \
       -sSv \
       https://ws.ifelse.io
...
> GET / HTTP/1.1
> Host: ws.ifelse.io
> Upgrade: websocket
> Sec-WebSocket-Key: e2dujvcbYbN747lapeH+WA==
> Sec-WebSocket-Version: 13
...
< HTTP/1.1 101 Switching Protocols
< Connection: upgrade
< upgrade: websocket
< sec-websocket-accept: 6wmMGMtN00aWw3loYd6P36EHKMI=
Run Code Online (Sandbox Code Playgroud)

其他选项是wscatwebsocat