如何在linux shell中读取WebSocket响应

oxcyiej7  于 2023-01-08  发布在  Linux
关注(0)|答案(5)|浏览(372)

编写一个bash脚本连接到GDAX的WebSocket Feed,网址是wss:ws-feed.gdax.com,但是curl似乎不支持这个功能。

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

2ic8powd1#

假设您已经安装了node,我会给予一下wscat;它"简单“、”直观“、”强大“,除此之外,@Pavel的答案还有很多值得尊敬的WebSocket客户端替代品。

# install
npm install -g wscat

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

qni6mghb3#

我想为此添加我自己的工具: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":[]}

除了websocket客户端,websocat还支持WebSocket服务器和其他模式,旨在将websocket集成到"UNIX"世界中。

wpcxdonn

wpcxdonn4#

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

vdzxcuhz

vdzxcuhz5#

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

telnet 120.22.37.128 6870

连接后,您可以以Json格式发送请求

{"requestType":"hi"}

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

sudo ufw allow 6870

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

pm2 logs 0

希望这能帮上忙。

相关问题