How to read websocket response in linux shell

后端 未结 5 1909
既然无缘
既然无缘 2020-12-24 07:18

Writing a bash script to connect to GDAX\'s Websocket Feed at wss://ws-feed.gdax.com but curl doesn\'t seem to support this as I get

curl \"wss://ws-feed.gd         


        
相关标签:
5条回答
  • 2020-12-24 07:28

    Well, you can try to mimic the required headers to get some response using curl:

    • https://gist.github.com/htp/fbce19069187ec1cc486b594104f01d0 or
    • Linux Bash: How to open a websocket connection as client

    Also, there are other ways to communicate with a WebSocket server, e.g.

    • https://github.com/websockets/wscat
    • https://github.com/bwasti/webpipe
    • https://github.com/progrium/wssh
    0 讨论(0)
  • 2020-12-24 07:29

    Assuming you have node installed, I would give wscat a shot; it is simple, intuitive, and powerful. Otherwise, @Pavel's answer has an abundance of venerable websocket client alternatives.

    # install
    npm install -g wscat
    
    # use
    wscat -c "wss://ws-feed.gdax.com"
    
    0 讨论(0)
  • 2020-12-24 07:29

    I'd like to add my own tool for this: websocat.

    Example session with the service in question:

    $ 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":[]}
    

    Besides a websocket client, websocat supports WebSocket server and other modes and is aimed to integrate websockets into "UNIX" world in general.

    0 讨论(0)
  • 2020-12-24 07:50

    You can connect to your server using Telnet also

         telnet 120.22.37.128 6870
    

    Once you connect you can send request in Json format

         {"requestType":"hi"}     
    

    Here 6870 is port port and to open port you need to run

         sudo ufw allow 6870 
    

    And to check pm2 logs you required to use below command

        pm2 logs 0
    

    Hope this hepls.

    0 讨论(0)
  • 2020-12-24 07:53

    ws start connection by http protocol, you have to change ws to http and some extra headers like this:

    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

    0 讨论(0)
提交回复
热议问题