websocat icon indicating copy to clipboard operation
websocat copied to clipboard

Execute a function depending of server response received

Open ellyssonmike opened this issue 4 years ago • 12 comments

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!

ellyssonmike avatar Mar 31 '21 19:03 ellyssonmike

It should be available as STDIN of the script.

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

vi avatar Mar 31 '21 19:03 vi

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.

ellyssonmike avatar Mar 31 '21 23:03 ellyssonmike

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"

vi avatar Apr 01 '21 00:04 vi

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!

ellyssonmike avatar Apr 01 '21 01:04 ellyssonmike

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?

Gabriele-LS avatar Apr 08 '21 06:04 Gabriele-LS

@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'

vi avatar Apr 08 '21 08:04 vi

@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 avatar Apr 08 '21 08:04 Gabriele-LS

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

Gabriele-LS avatar Apr 08 '21 10:04 Gabriele-LS

@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 avatar Apr 08 '21 14:04 vi

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.

vi avatar Apr 08 '21 14:04 vi

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

Gabriele-LS avatar Apr 08 '21 14:04 Gabriele-LS

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.

vi avatar Apr 14 '21 23:04 vi