vi / websocat

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

Execute a function depending of server response received #114

Open EllyssonMike opened 3 years ago

EllyssonMike commented 3 years ago

Hi, Vi!

Please, sorry my bad english. I'm brazilian.

I'm working with shellscript file and inside of this file I need to get the incomming message.

/ foreach-message.sh /

if [ $incommingMessage == 'update-system' ]; then
updateSystem();
fi

if [ $incommingMessage == 'update-playlist' ]; then
updatePlaylist();
fi

if [ $incommingMessage == 'send-to-mysql' ]; then
sendToMySql();
fi

I've tried: websocat -t ws://localhost:8080 foreachmsg:exec:./foreach-message.sh

But, I don't now how to pass the incomming message to ./foreach-message.sh

Thanks a lot!

vi commented 3 years ago

It should be available as STDIN of the script.

Try to add read incommingMessage at the beginning of the script.

EllyssonMike commented 3 years ago

It should be available as STDIN of the script.

Try to add read incommingMessage at the beginning of the script.

Hi, Vi. Thank you for your answer.

I've tried this, but not working with me.

My test script: I've tried with #!/bin/sh at the beginning and without it.

`#!/bin/sh read incomingMessages;

if [ $incomingMessages == "exit" ]; then shutdown now fi `

and on terminal: websocat -t ws://localhost:8080 foreachmsg:exec:./testscript.sh

The file testscript.sh is launched, but nothing happens.

I've tested too: websocat -t ws://localhost:8080 | ./testscript.sh

This works, but only one time.

** EDIT

If I put on terminal: websocat -t ws://localhost:8080, the console retrieve all information in real time. But if I use foreachmsg:exec:./testscript.sh nothing happens. The terminal stays freeze. If some user send a message, the message not appear in the console. I can only retrieve the messages if I use just websocat -t ws://localhost:8080. Without foreachmsg:exec:./testscript.sh at the end.

vi commented 3 years ago

Something may indeed be wrong with foreachmsg: overlay - it causes the launched process to immediately terminate.

Current workaround may be like this:

websocat -t ws://localhost:8080 exec:/usr/bin/xargs --exec-args -i sh -c "echo '{}' | ./testscript.sh"
EllyssonMike commented 3 years ago

Something may indeed be wrong with foreachmsg: overlay - it causes the launched process to immediately terminate.

Current workaround may be like this:

websocat -t ws://localhost:8080 exec:/usr/bin/xargs --exec-args -i sh -c "echo '{}' | ./testscript.sh"

OMG! This saved my life. I have no words to thank you! Thank you a lot, bro. Success!

Gabriele-LS commented 3 years ago

Hello! @vi I'm using the following command:

websocat -t ws://127.0.0.1:8989/api/v1/socket exec:xargs --exec-args -I{} sh -c "echo '{}' > ~/myTestDoc.txt"

My aim is to call a script (using "| /myScript.sh" in place of "> ~/myTestDoc.txt") and have that script receive the message coming from the websocket.

Unfortunately, in the myTestDoc.txt I just find "{}". Could you please help me?

vi commented 3 years ago

@Gabriele-LS I tried the command line above and it works for me - the file is updated with each incoming WebSocket message. Are you using GNU/Linux?

What xargs --version does output for you? Mine has xargs (GNU findutils) 4.8.0.


Here is another workaround that works for me:

/opt/websocat -t ws://127.0.0.1:1234/api/v1/socket cmd:'while read INPUT; do echo $INPUT > /tmp/test.txt; done'
Gabriele-LS commented 3 years ago

@vi Thanks for your reply. That last command works. The previous one doesn't. I'm using macOS and the xargs --version command output this error: ‘xargs: illegal option -- -‘.

Anyway, both the commands execute multiple times (one time per each paragraph received thru the websocket). Is there any chance to make the command run only once per each group of messages (messages arriving all together, split in multiple paragraphs)?

Gabriele-LS commented 3 years ago

Sorry @vi , I was wrong. The execution is correct. The command is executed each time a message comes thru the websocket.

vi commented 3 years ago

@Gabriele-LS What is a paragraph? Two \n (0A) bytes in a row? In those workaround methods it is an external program (xargs or bash's read command) decides how to split incoming text into chunks for execution.

vi commented 3 years ago

Note that the problems may arise if messages would contain whitespace characters. You can use websocat's and xargs's -0 option (or similar IFS trick in shell) to prevent this.

Gabriele-LS commented 3 years ago

@vi I was wrong. There is no issue related to paragraphs.

vi commented 3 years ago

Fixed child process handling in Websocat so that processed do not get terminated prematurely. foreachmsg:exec: should now work properly.

Also implemented --foreachmsg-wait-read to allow reliable reply messages from foreachmsg: overlay.