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

Transfer message when received #143

Closed vegalou closed 2 years ago

vegalou commented 2 years ago

Post Message

Thanks for your efforts on Websocat client.

Is it possible to send received message as websocket event comes.

ig:

When websocat received a message `{"a":123, "b":456}, then it cound be forward a POST to http or process by ShellScript.

like

websocat "ws:/foo/myChannel" exec:'whereToPostOrCommand'

Here I wound to try is forward a message as soon as message comes

vi commented 2 years ago

Websocat 1 can execute shell commands based on incoming WebSocket messages. Here is example command line that converts each incoming WebSocket message to a HTTP POST request using curl:

websocat  -u -t ws-l:127.0.0.1:1234 reuse:autoreconnect:foreachmsg:sh-c:'curl -s -X POST http://127.0.0.1:1235/ -d@-'

Although Websocat 1 has internal http-client: overlay, it is not suitable for this task.

Websocat 3 can turn datagrams into HTTP requests directly, but is still missing too many parts (like HTTP request acceptor, hence incoming WebSocket connections acceptor) to make this working.

vegalou commented 2 years ago

Dear Vitaly

I would test your suggest every steps you recommend.

Thanks for your help sincerelly.

vi commented 2 years ago

Just noticed that you maybe means forwarding messages received by WebSocket client, not server.

Modified command line would be:

websocat -nu   -t ws://127.0.0.1:1234/ foreachmsg:sh-c:'curl -s -X POST http://127.0.0.1:1235/ -d@-'

Websocat 3 can do it directly, without going through curl:

websocat3  '[session unidirectional_reverse=true left=[wsc uri=ws://127.0.0.1:1234/] right=[spawner no_replies=true early_drop=true @ http-client buffer_request_body=true stream_request_body=true uri=http://127.0.0.1:1235]]'

This command line may stop working when websocat3 gets released. Simpler command lines like websocat3 -U ws://127.0.0.1:1234 foreachmsg:http://127.0.0.1:1234 are planned to be eventually supported.