vi / websocat

Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
MIT License
7.18k stars 278 forks source link

How to connect and send request in command line ? #42

Open Mto05 opened 5 years ago

Mto05 commented 5 years ago

Hi.

Thank for this tool !

I want to launch in command line websocat with a request. I try this :

websocat ws://url:port | echo 'request in JSON'

But not work.

I want to record the result in a file. How i do ?

Sorry for my bad english ...

Thank for your reply and your help.

vi commented 5 years ago

Maybe you want to reverse the command line pipeline?

echo 'request in JSON' | websocat ws://url:port > reply.json

Notes:

  1. If WebSocket is getting closed too early (before reply arrives), try websocat -n1 ws://...
  2. If that JSON is specifically JSONRPC 2.0, you may find --jsonrpc option helpful.
Mto05 commented 5 years ago

Hi.

Thank for your help.

echo 'request in JSON' | websocat -n ws://url:port > reply.json work successfuly.

Is that possible to launch websocat as daemon for continue data record in the file ?

i don't find this information in help.

Thank you for your help.

vi commented 5 years ago

Yes. How do you want to contact this daemon? Over TCP or UNIX socket?

For example,

websocat -Et tcp-l:127.0.0.1:1234 reuse-raw:ws://echo.websocket.org

acts as a server, forwarding multiple incoming TCP connections to one reused WebSocket connection. Each line of incoming data is transformed to a WebSocket message, each incoming WebSocket message is transformed into a line, which is sent to some currently connected client (or cached up and sent to subsequent client if current client suddenly disconnected).

echo 'request in JSON' | nc -q 1 127.0.0.1 1234 > reply.json

acts as a client (you can also use something like websocat -b - tcp:127.0.0.1:1234 as a TCP client, but there may be some issues).

vi commented 5 years ago

I don't find this information in help.

Maybe client-server case with persistent WebSocket connection should be documented in moreexamples.md?

Mto05 commented 5 years ago

Hi.

i did read moreexamples.md and i didn't find how lauch websocat as daemon.

But i did proceed like this and that work : echo 'command' | websocat -n ws://ws.link.org:port > file.json &

When I will have more time I will test your solution.

Thank for your help and your replies.

Cordially.

mirspasaka commented 5 years ago

Hallo, friends!
I've got some question for using WEBSOCAT as client. There are conditions:

  1. ws server is $WS_SERVER
  2. I need ask $ASK every second $WS_SERVER for $COMMAND in response for my $ASK
  3. Now i every time make
    while : do echo $ASK | websocat $WS_SERVER > command.json sleep 1 done Please, give me advice^ can i websocat $WS_SERVER one time and then just send to this json as > request. or i did all correct and every time as client need to echo $ASK | websocat $WS_SERVER

p.s. thanks a lot for websocat =)

vi commented 5 years ago

@mirspasaka, I'm not sure I understand the question properly.

I need ask $ASK every second $WS_SERVER for $COMMAND in response for my $ASK

It is not clear what is an ASK (acknowledgement? query?) and what is COMMAND. Seems like ASK is a query. COMMAND seems to be the name of some output file...

while : do echo $ASK | websocat $WS_SERVER > command.json sleep 1 done

Assuming this is [a part] of Bash script with missing newlines after do, json and sleep 1.

advice^ can i websocat $WS_SERVER

Assuming the circumflex and asterisks are non-relevant.

one time and then just send to this json as > request

Assuming this means "connect to Websocket server once, keep the connection alive, send multiple periodic requests to it and append replies to a file".


If all my assumpions are correct, then your command line should be like this:

while true; do echo "$ASK"; sleep 1; done | websocat "$WS_SERVER" > commands.txt

Each input line turns into one text WebSocket message, each received WebSocket text message turns into exactly 1 line of commands.txt (which would unfortunately be invalid JSON).

mirspasaka commented 5 years ago

i thought 'websocat' acts like exec websocat '-H Authorization: Basic asfdsadfsdf' ws://server:8080/json_answer 3<> and after that can send requests like echo $request >&3 because connection is open for ws server.

vi commented 5 years ago

acts like exec

What is to act like exec?

websocat ... 3<> >&3

If you want websocat to use specified file descriptor, use fd-open: specifier:

websocat '-H Authorization: Basic asfdsadfsdf' --text fd-open:3 ws://server:8080/json_answer

The descriptor should be epoll-able,

If you just want persistently running websocket client, with ability to connect to it, send WebSocket message, receive WebSocket reply, disconnect (but not close WebSocket connection), you can use multiple websocats like this:

# Start persistent client connection, with Linux's abstract-namespaced UNIX socket at the other end
# Note in reality the WebSocket connection starts only on first incoming UNIX connection.
websocat -E --text abstract-listen:qqq reuse-raw:ws://127.0.0.1:1234/ &

while true;  do
     # Send current as a text WebSocket message, just for fun.
     date | websocat -1u -b - abstract-connect:qqq;

     # Wait for one incoming message from a WebSocket, print it to terminal
     websocat -1U -b - abstract-connect:qqq;

     sleep 1;
done

If multiple such loops run in parallel then replies may be confused.

reuse-raw: instead of usual reuse: makes reply wait until second connection comes in instead of dropping the reply.

Entire scheme may be not very reliable in general. unix-listen:/unix-connect: may be used on non-Linux UNIX instead of abstract-*:.

mirspasaka commented 5 years ago

If multiple such loops run in parallel then replies may be confused.

wow. so many issues to test! ThanksoMUCH, thats all i wanted to try =)

mirspasaka commented 5 years ago

I understand this part:

while true; do

Send current as a text WebSocket message, just for fun.

 date | websocat -1u -b - abstract-connect:qqq;

 # Wait for one incoming message from a WebSocket, print it to terminal
 websocat -1U -b - abstract-connect:qqq;

 sleep 1;

done



If multiple such loops run in parallel then replies may be confused.

`reuse-raw:` instead of usual `reuse:` makes reply wait until second connection comes in instead of dropping the reply.

Entire scheme may be not very reliable in general. `unix-listen:`/`unix-connect:` may be used on non-Linux UNIX instead of `abstract-*:`.

But can't understand how create connect for

websocat -E --text abstract-listen:qqq reuse-raw:ws://127.0.0.1:1234/ &

if ws address is not local/ ws://1.2.3.4:1234/websocket and what is "abstract-listen:qqq"

felixriehm commented 1 year ago

shouldn't this issue be auto-closed because of inactivity? and also because the original question has been answered.

vi commented 1 year ago

I rarely close others' issues here in general, typically they keep accumulating unless issue authors decides to close them.

If the question is answered, does it really matter whether issue status stays open or changes to closed?

auto-closed

When you see auto-closed issues on Github, it is currently action of a special bot, not an inherent feature of Github.

mirspasaka commented 1 year ago

ALL JOBS DONE! very nice. i don't know how to close

Zibri commented 6 months ago

what's the simplest way to send a text file line by line after the connection?

mirspasaka commented 6 months ago

what's the simplest way to send a text file line by line after the connection? i use 'nc' command for this launching a websocket as a separate process like websocat -Et -B 524228 tcp-l:127.0.0.1:1234 -H=Authorization: Basic PASWWWORD -H=SOMEHEAD: HEAD --ping-interval 5 --ping-timeout 10 reuse-raw:autoreconnect:wss://wsserver.net:443

echo "yourtext" | nc -w 1 127.0.0.1 1234 so answer is: while IFS= read -r line; do echo "Text read from file: $line" | nc -w 1 127.0.0.1 1234 done < my_filename.txt

something this way