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

Shutdown not working? #308

Open
Jasperav opened this issue Dec 30, 2019 · 2 comments
Open

Shutdown not working? #308

Jasperav opened this issue Dec 30, 2019 · 2 comments

Comments

@Jasperav
Copy link

Jasperav commented Dec 30, 2019

This is a reproduction project: https://github.com/Jasperav/WebsocketRust, this is main.rs:

use ws::{listen, connect};
use std::thread::{spawn, sleep};
use std::time::Duration;

fn main() {
    run_ws();

    connect("ws://127.0.0.1:3042", |c| {
        c.send(""); // Don't know why something needs to be send to open up the connection
        sleep(Duration::from_secs(1));
        move |m| {
            c.shutdown().unwrap();
            Ok(())
        }
    }).unwrap();

    run_ws();
}

fn run_ws() {
    let _ = spawn(|| {
        listen("127.0.0.1:3042", |c| {
            move |msg| {
                c.send(""); // Send something back
                // Not sure why I can not shutdown the websocket on the instance of the websocket itself.
                Ok(())
            }
        }).unwrap();
    });

    sleep(Duration::from_secs(1)); // Let server startup
}

Why isn't the shutdown method working? The program crashes because the connection isn't closed.

@Jasperav
Copy link
Author

Jasperav commented Dec 30, 2019

Even with the example from here: https://github.com/housleyjk/ws-rs/blob/master/examples/external_shutdown.rs, I can not get it to work with the following code:

use ws::{listen, connect, Sender, Handler, Message, Handshake};
use std::thread::{spawn, sleep};
use std::time::Duration;
use std::sync::Arc;

struct Client {
    c: Sender,
    b: Arc<Sender>
}

impl Handler for Client {
    fn on_open(&mut self, _: Handshake) -> Result<(), ws::Error> {
        self.c.send("") // Send a message to open the connection, weird
    }

    fn on_message(&mut self, msg: Message) -> Result<(), ws::Error> {
        self.b.shutdown().unwrap();
        self.c.shutdown().unwrap();

        Ok(())
    }
}

fn main() {
    let s = run_ws();
    let arc = Arc::new(s);

    connect("ws://127.0.0.1:3042", |c| {
        Client {
            c,
            b: Arc::clone(&arc)
        }
    }).unwrap();

    run_ws();
}

fn run_ws() -> Sender {
    let socket = ws::Builder::new().build(|c: ws::Sender| {
        move |msg| {
            c.send(""); // Send something back
            // Not sure why I can not shutdown the websocket on the instance of the websocket itself.
            Ok(())
        }
    }).unwrap();

    let broadcast = socket.broadcaster();

    let _ = spawn(move || {
        socket.listen("127.0.0.1:3042").unwrap();
    });

    sleep(Duration::from_secs(1)); // Let server startup

    broadcast
}

I am calling shutdown twice and the listener is never closed

@mverleg
Copy link

mverleg commented Apr 25, 2021

You're starting the server twice (run_ws() at the start and end of main in both examples).

Perhaps it does shut down once, but gets started again? The second one never shuts down, because shutdown happens when it receives a message.

Calling shutdown twice doesn't help because it's twice on the same server.

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