Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminate Individual WebSocket Connection #298

Open
isaaczinda opened this issue Nov 18, 2019 · 2 comments
Open

Terminate Individual WebSocket Connection #298

isaaczinda opened this issue Nov 18, 2019 · 2 comments

Comments

@isaaczinda
Copy link

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?

@shinglyu
Copy link

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();
}

@shinglyu
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants