vi / websocat

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

Feature request: Allow setting connection close code #149

Open vbsd opened 2 years ago

vbsd commented 2 years ago

Unless I'm missing something, there seems to be no way to set connection close code with websocat. This is useful as a way to indicate session success/failure out-of-band.

vi commented 2 years ago

Indeed, there is currently no way in Websocat to indicate the code. -n option only allows to omit the close frame entirely.

This feature may be added in websocat3; less likely to be added in websocat1.

vbsd commented 2 years ago

Would you consider accepting a pull request with this option to websocat1? Do you have any tips for giving a try to implement this? No promises, probably nothing's going to come out of it, but I'm somewhat interested.

vi commented 2 years ago

For a quick experiment, you can just hard code it like this:

diff --git a/src/ws_peer.rs b/src/ws_peer.rs
index 5aa3692..9efbc57 100644
--- a/src/ws_peer.rs
+++ b/src/ws_peer.rs
@@ -212,7 +212,7 @@ impl<T: WsStream + 'static> AsyncWrite for WsWriteWrapper<T> {
         }
         let mut sink = self.sink.borrow_mut();
         match sink
-            .start_send(OwnedMessage::Close(None))
+            .start_send(OwnedMessage::Close(Some(websocket::CloseData{status_code: 500, reason: "An error".to_owned()})))
             .map_err(io_other_error)?
         {
             futures::AsyncSink::NotReady(_) => wouldblock(),

(only checked that it builds, not that it works)

If I'll be doing websocat 1.10.0, I'll probably integrate it as a CLI option (maybe with some of other requested changes).

vbsd commented 2 years ago

Nice, I'll try it out, thank you!

vbsd commented 2 years ago

This PR implements close status options: https://github.com/vi/websocat/pull/151 @vi Would you be able to merge this? (Or something like this - I can adjust.)