vi / websocat

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

How do websocat know the other one reached EOF ? #235

Closed AlwaysNoobCoder closed 2 months ago

AlwaysNoobCoder commented 2 months ago
-E, --exit-on-eof                           Close a data transfer direction if the other one reached EOF

how do websocat know the other one reached EOF ?

AlwaysNoobCoder commented 2 months ago

@vi maybe this is silly, but can you give some inputs ?

vi commented 2 months ago

Typically when recv(3) or read(3) syscall returns 0 bytes or maybe after receiving WebSocket close message.

By default (without -E) it waits for the other direction to also return 0 bytes before considering the connection as finished. With -E it drops possible future or pending data and closes the other direction immediately.

For example, in this invocation:

websocat wss://ws.vi-server.org/mirror

there are two directions:

  1. Reading from stdin and writing to a WebSocket
  2. Reading from the WebSocket and writing to stdout

If you press Ctrl+D, you cause stdin to signal end-of-file, so it finishes the first direction. If WebSocket sends close message or drops the connection, it finishes the second direction.

AlwaysNoobCoder commented 2 months ago

@vi thanks! without -E , websocat command will not exit, even the server already closed the websocket session, this behaviour is a bit confusing for me.