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

Using foreachmsg and a Unix socket - syntax question #131

Closed bjesus closed 2 years ago

bjesus commented 2 years ago

Hello and thanks for websocat!

I'm currently using websocat -Et unix-l:/tmp/mysocket.socket reuse-raw:wss://my.web.socket/endpoint to communicate with my websocket server over a UNIX socket. Sending messages to the websockets server works great, but I want to also do something whenever a message is received. I'm trying to use foreachmsg but can't seem to figure out where exactly to place it. I want websocat to pass the incoming messages to my script. what's the right way of doing this? Is foreachmsg even the right approach?

I've tried things like websocat --foreachmsg-wait-read -Et unix-l:/tmp/mysocket.socket foreachmsg:cmd:./send-keys.sh:reuse-raw:wss://my.web.socket/endpoint but without luck, my syntax seems to be wrong. Is it because I also specified a Unix socket?

Thanks again!

vi commented 2 years ago

foreachmsg: means "re-establish connection for each incoming buffer of data" (where a buffer of data may correspond to a WebSocket message, one portion of data returned by a TCP socket or other byte-oriented stream, UDP datagram). It may be used to make Websocat start some script again and again for each incoming WebSocket message instead of using one long-running script to serve all messages in that WebSocket connection.

cmd:./send-keys.sh:reu...

cmd: is a terminal specifier, it does not expect inner nodes. All of the ./send-keys.sh:reuse-raw:wss://my.web.socket/endpoint may be interpreted as a script.

websocat --foreachmsg-wait-read -Et unix-l:/tmp/mysocket.socket foreachmsg:cmd:./send-keys.sh:reuse-raw:wss://my.web.socket/endpoint

I see three endpoints here:

  1. UNIX socket acceptor
  2. Shell script send-keys.sh
  3. Client WebSocket connection.

Websocat (like Socat) typically interconnects two endpoints. Maybe you want to split outgoing and incoming WebSocket messages and process them separately? I.e. outgoing messages are taken from a UNIX socket and incoming messages are processed by the script? In this case just one WebSocat invocation is probably not enough - you need to chain multiple things. Websocat3 would have that feature natively, but currently you'll need to use something like this:

/opt/websocat -tE unix-l:/tmp/mysocket.socket reuse-raw:sh-c:'/opt/websocat -t - wss://echo.websocket.org | xargs -n1 echo'

Messages are sources from UNIX socket, then go though a WebSocket and back, then go though a script (echo), replies from script go back to UNIX socket.

You can use websocat -u -t - foreachmsg:cmd: instead of xargs if needed, but it may work worse - e.g. I cannot get it to propagate replies properly. foreachmsg: is one of the trickier and glitchier overlays.

bjesus commented 2 years ago

Awesome, splitting outgoing and incoming messages works perfectly! Thank you very much for your help 🙏