ws-rs
ws-rs copied to clipboard
Terminate Individual WebSocket Connection
I've scoured the docs and haven't found a way to terminate a single connection between a ws Rust server and a client. I'm trying to do this from the server-side.
Sending a close message is nice when the client is still connected, but I am interested in handling the case when the client's network connection drops out. In this case, sending a close message won't actually close the connection because the client is disconnected and can't sent a response.
The ws library in Node.js provides ws.terminate()
to forcibly terminate a connection with a client. Does the ws library in Rust have a similar method or some way to terminate a connection?
This will be a useful feature.
In the ping-pong example, if I try to connect with this bad client that doesn't respond to pings, the connection will not be closed and the ping will go on until the connection is eventually dropped.
extern crate ws;
use ws::{connect, Frame, CloseCode, Message, OpCode, Result};
struct Client {
out: ws::Sender,
}
impl ws::Handler for Client {
fn on_frame(&mut self, frame: Frame) -> Result<Option<Frame>> {
if frame.opcode() == OpCode::Ping{
println!("Received a ping, but we are not responding");
}
Ok(None)
}
}
fn main() {
connect("ws://127.0.0.1:8080", |out| {
Client {
out: out,
}
}).unwrap();
}
I took a look at the Sender::shutdown()
function, it basically loops through all the connections and terminates them. Is it possible to identify a particular connection (by Token
maybe?) inside the connections
array and add a new function to terminate only one?