如何在bash中阅读websocket响应

时间:2017-12-18 00:10:11

标签: bash curl websocket

在wss://ws-feed.gdax.com上写一个bash脚本连接到GDAX's Websocket Feed,但curl似乎不支持这个,因为我得到了

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

5 个答案:

答案 0 :(得分:18)

假设你安装了node,我会给wscat一个机会;它是简单直观强大的。否则,@ Pavel的答案有很多令人尊敬的websocket客户端替代品。

# install
npm install -g wscat

# use
wscat -c "wss://ws-feed.gdax.com"

答案 1 :(得分:9)

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

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

答案 2 :(得分:6)

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

与相关服务的示例会话:

$ rlwrap  websocat wss://ws-feed.gdax.com
{"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"}
{"type":"unsubscribe","channels": [{ "name": "heartbeat", "product_ids": ["BTC-USD"] }]}
{"type":"subscriptions","channels":[]}

除了websocket客户端外,websocat还支持WebSocket服务器和其他模式,旨在将websockets集成到一般的“ UNIX”世界中。

答案 3 :(得分:0)

您也可以使用 Telnet 连接到服务器

     telnet 120.22.37.128 6870

连接后,您可以使用 Json 格式

发送请求
     {"requestType":"hi"}     

此处 6870 是端口,要打开该端口,您需要运行

     sudo ufw allow 6870 

要检查您需要使用以下命令的pm2日志

    pm2 logs 0

希望这个帮助。

答案 4 :(得分:0)

ws通过http协议开始连接,您必须将ws更改为http和一些额外的标头,如下所示:

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"

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