I have created a Node.JS application that provides a web socket server (using npm ws). This websocket waits until a connection is established. Once a connection hits the server, the websocket executes a job. When the job is done, a message is sent over the socket, then the socket is being closed. This socket works as expected; already tested it with another Node.JS script.
How can I connect to the web socket using only linux command line tools? I already tried curl as described here. However, I can not find out how to connect properly to my websocket which runs at localhost:8088/socket/
Edit: My question has been identified as a possible duplicate of another question. However, the linked question only asks if there is a way to do it with curl. I'd be glad to see any solution which works on bash. Also, the answer to the linked question is a javascript file using autobahn.ws
To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new WebSocket("ws://javascript.info"); There's also encrypted wss:// protocol. It's like HTTPS for websockets.
In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.
My tool websocat is specifically designed for this.
websocat ws://your_server/url
You can connect and exchange data with your server. By default each line becomes a WebSocket text message and vice versa.
On Linux it is more comfortable to play with it using readline:
rlwrap websocat ws://your_server/url.
It is not the only CLI websocket client. There are also "ws" and "wscat" projects.
Try this one from here: How to hit the WebSocket Endpoint?
$ curl -i -N \
-H "Connection: Upgrade" \
-H "Upgrade: websocket" \
-H "Host: echo.websocket.org" \
-H "Origin: http://www.websocket.org" \
http://echo.websocket.org
Which he has from here: http://www.thenerdary.net/post/24889968081/debugging-websockets-with-curl
To quote the content from this site for the future:
Those flags say:
- Return headers in the output
- Don’t buffer the response
- Set a header that this connection needs to upgrade from HTTP to something else
- Set a header that this connection needs to upgrade to a WebSocket connection
- Set a header to define the host (required by later WebSocket standards)
- Set a header to define the origin of the request (required by later WebSocket standards)
If the websocket is working it should return the following:
$ curl -i -N \
-H "Connection: Upgrade" \
-H "Upgrade: websocket" \
-H "Host: echo.websocket.org" \
-H "Origin:http://www.websocket.org" \
http://echo.websocket.org
HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
WebSocket-Origin: http://www.websocket.org
WebSocket-Location: ws://echo.websocket.org/
Server: Kaazing Gateway
Date: Mon, 11 Jun 2012 16:34:46 GMT
Access-Control-Allow-Origin: http://www.websocket.org
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Headers: x-websocket-extensions
Access-Control-Allow-Headers: x-websocket-version
Access-Control-Allow-Headers: x-websocket-protocol
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With