vi / websocat

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

pipe commands to websocat? #245

Closed AntonioSun closed 4 months ago

AntonioSun commented 4 months ago

Does websocat support accepting commands from pipe?

JFTA, nc can, in two different ways:

nc -q -1 host port:

-q seconds after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.

or,

echo command | netcat host port -

Thanks for considering.

vi commented 4 months ago

By default WebSocat, when sees EOF on stdin, sends a WebSocket close frame, then waits for possible final replies, prints them, then exits.

-n opts out from sending the close frame (you may need to use this one). -E makes it exit immediately when stdin is closed, without waiting for replies at all.

Note that by default WebSocat is in text and line-based mode, so number of WebSocket text messages sent should be equal to number of lines on input.

AntonioSun commented 4 months ago

Thanks!

vi commented 4 months ago

Another influential option (for servers which fail to reply if they receive the close frame immediately after request message) is -1 (--one-message):

$ echo '{"email":"user1@mail.com","password":"test"}' | websocat -n -1 ws://ws.vi-server.org/mirror/ | wc
      1       1      45
$

Also you can somewhat simulate a -q option of netcat like this:

$ { echo '{"email":"user1@mail.com","password":"test"}'; sleep 1; } | /opt/websocat -E  ws://ws.vi-server.org/mirror/ | wc
(1 second delay)
      1       1      45
$