Reputation: 2011
I would like to connect to a server using WebSockets but do not want to pull in a complex environment like Python just to repeatedly send small pieces of data over the wire (this is not a fast computer and Python sometimes takes a noticeable moment or two for itself to initialize each time).
That said, WebSockets is a sufficiently complex protocol - it incorporates transparent ping/pong, framing and other (admittedly pretty awesome) features - that it would take quite a while to implement enough of the protocol to make a usable client from scratch.
I'm looking for something which opens the socket, handles the underlying protocol and lets me handle just the data I need to send, kind of like "websocket netcat".
(Context: I need to talk to Chrome's WebSocket-based remote debugging server from the commandline.)
EDIT: So far I've found this proof-of-concept example written in C which uses libcurl; protocol-wise, it would have to be the simplest way to get started with cURL handling the HTTP keepalives and so forth (cURL doesn't speak WebSockets itself yet), but for some reason Chrome is not responding to this program's instructions at all. I've managed to get it to send identical headers to the Chrome remote debugger via WireShark, but it's still sending differently formatted data.
Chrome's debugger's request header starts with 81 ac 99 2f fa 22 e2 0d 97 ....
, whereas the C program sends 81 1f 7b 22 69 64 22 3a 31 ....
. The 81
is identical, at least! :P Guess I have to go learn the binary WebSocket protocol now...
The actual JSON command I am sending over the wire is {"id":1,"method":"Page.reload"}
, as per this python reloader (where I got the idea from) and confirmed to be the correct command via this Ruby-based remote control library.
Any takers? :P
Upvotes: 2
Views: 5079
Reputation: 38704
I'm looking for something which opens the socket, handles the underlying protocol and lets me handle just the data I need to send, kind of like "websocket netcat".
My new tool websocat is explicitly advertised as "websocket netcat".
nc -e
analogue is also supported: you can start your program and it will be connected to WebSocket with stdin/stdout.
Example using Chrome debugging port:
chromium --remote-debugging-port=9222&
ws=`curl -sg http://127.0.0.1:9222/json/new | grep webSocketDebuggerUrl | cut -d'"' -f4 | head -1`
echo '{"id":2,"method":"Page.navigate","params":{"url":"https://example.com"}}' | websocat -n1 $ws
Upvotes: 0
Reputation: 366
The library closes the connection right after it sends data, that's why you don't receive any answer. Don't call the function curl_easy_cleanup
while you are working, cause it will close the connection.
Upvotes: 1