pub trait Factory {
    type Handler: Handler;
    fn connection_made(&mut self, _: Sender) -> Self::Handler;

    fn on_shutdown(&mut self) { ... }
fn client_connected(&mut self, ws: Sender) -> Self::Handler { ... }
fn server_connected(&mut self, ws: Sender) -> Self::Handler { ... }
fn connection_lost(&mut self, _: Self::Handler) { ... } }
Expand description

A trait for creating new WebSocket handlers.

Associated Types

Required methods

Called when a TCP connection is made.

Provided methods

Called when the WebSocket is shutting down.

Called when a new connection is established for a client endpoint. This method can be used to differentiate a client aspect for a handler.

use ws::{Sender, Factory, Handler};

struct MyHandler {
    ws: Sender,
    is_client: bool,
}

impl Handler for MyHandler {}

struct MyFactory;

impl Factory for MyFactory {
    type Handler = MyHandler;

    fn connection_made(&mut self, ws: Sender) -> MyHandler {
        MyHandler {
            ws: ws,
            // default to server
            is_client: false,
        }
    }

    fn client_connected(&mut self, ws: Sender) -> MyHandler {
        MyHandler {
            ws: ws,
            is_client: true,
        }
    }
}

Called when a new connection is established for a server endpoint. This method can be used to differentiate a server aspect for a handler.

use ws::{Sender, Factory, Handler};

struct MyHandler {
    ws: Sender,
    is_server: bool,
}

impl Handler for MyHandler {}

struct MyFactory;

impl Factory for MyFactory {
    type Handler = MyHandler;

    fn connection_made(&mut self, ws: Sender) -> MyHandler {
        MyHandler {
            ws: ws,
            // default to client
            is_server: false,
        }
    }

    fn server_connected(&mut self, ws: Sender) -> MyHandler {
        MyHandler {
            ws: ws,
            is_server: true,
        }
    }
}

Called when a TCP connection is lost with the handler that was setup for that connection.

The default implementation is a noop that simply drops the handler. You can use this to track connections being destroyed or to finalize state that was not internally tracked by the handler.

Implementors