如何使用cURL读取WebSocket响应

b5lpy0ml  于 2023-01-30  发布在  其他
关注(0)|答案(6)|浏览(714)

编写一个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
vxqlmq5t

vxqlmq5t1#

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

# install
npm install -g wscat

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

ie3xauqp3#

我想为此添加我自己的工具: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"世界中。

mi7gmzs6

mi7gmzs64#

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

voj3qocg

voj3qocg5#

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

telnet 120.22.37.128 6870

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

{"requestType":"hi"}

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

sudo ufw allow 6870

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

pm2 logs 0

希望这能帮上忙。

sbdsn5lh

sbdsn5lh6#

考虑到这里有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=

Upgrade使其从HTTP(s)切换到WebSocket协议。
Connection指定Upgrade是一个逐跳报头(中介应该使用而不是转发的报头),但我的实验表明,没有这个报头也可以工作,或者更准确地说,如果服务器前面有一个反向代理(例如nginx),它可能是可选的。
Sec-WebSocket-Key/Sec-WebSocket-Accept是这里、这里和这里描述的安全措施。
Sec-WebSocket-Version指定WebSocket协议版本。根据RFC 6455,它应该等于13。
当客户端是浏览器并且请求页面的来源与WebSocket服务器URL的来源不匹配时,需要Origin
关于WebSocket握手请求的详细描述可以在这里找到。
HTTP/1.1就是这样的,HTTP/2是一个different story
了解这一点后,可以使用curl建立WebSocket连接:

$ 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=

其他选项为wscatwebsocat

相关问题