[][src]Struct websocket::server::WsServer

pub struct WsServer<S, L> where
    S: OptionalTlsAcceptor
{ pub ssl_acceptor: S, // some fields omitted }

Represents a WebSocket server which can work with either normal (non-secure) connections, or secure WebSocket connections.

This is a convenient way to implement WebSocket servers, however it is possible to use any sendable Reader and Writer to obtain a WebSocketClient, so if needed, an alternative server implementation can be used.

Synchronous Servers

Synchronous implementations of a websocket server are available below, each method is documented so the reader knows whether is it synchronous or asynchronous.

To use the synchronous implementation, you must have the sync feature enabled (it is enabled by default). To use the synchronous SSL implementation, you must have the sync-ssl feature enabled (it is enabled by default).

Asynchronous Servers

Asynchronous implementations of a websocket server are available below, each method is documented so the reader knows whether is it synchronous or asynchronous. Simply look out for the implementation of Server whose methods only return Futures (it is also written in the docs if the method is async).

To use the asynchronous implementation, you must have the async feature enabled (it is enabled by default). To use the asynchronous SSL implementation, you must have the async-ssl feature enabled (it is enabled by default).

A Hyper Server

This crates comes with hyper integration out of the box, you can create a hyper server and serve websocket and HTTP on the same port! check out the docs over at websocket::server::upgrade::sync::HyperRequest for an example.

A Custom Server

So you don't want to use any of our server implementations? That's O.K. All it takes is implementing the IntoWs trait for your server's streams, then calling .into_ws() on them. check out the docs over at websocket::server::upgrade::sync for more.

Fields

The SSL acceptor given to the server

Methods

impl WsServer<NoTlsAcceptor, TcpListener>
[src]

Asynchronous methods for creating an async server and accepting incoming connections.

Bind a websocket server to an address. Creating a websocket server can be done immediately so this does not return a Future but a simple Result.

Turns the server into a stream of connection objects.

Each item of the stream is the address of the incoming connection and an Upgrade struct which lets the user decide whether to turn the connection into a websocket connection or reject it.

See the [examples/async-server.rs] (https://github.com/cyderize/rust-websocket/blob/master/examples/async-server.rs) example for a good echo server example.

impl WsServer<TlsAcceptor, TcpListener>
[src]

Asynchronous methods for creating an async SSL server and accepting incoming connections.

Bind an SSL websocket server to an address. Creating a websocket server can be done immediately so this does not return a Future but a simple Result.

Since this is an SSL server one needs to provide a TlsAcceptor that contains the server's SSL information.

Turns the server into a stream of connection objects.

Each item of the stream is the address of the incoming connection and an Upgrade struct which lets the user decide whether to turn the connection into a websocket connection or reject it.

See the [examples/async-server.rs] (https://github.com/cyderize/rust-websocket/blob/master/examples/async-server.rs) example for a good echo server example.

impl<S> WsServer<S, TcpListener> where
    S: OptionalTlsAcceptor
[src]

Synchronous methods for creating a server and accepting incoming connections.

Get the socket address of this server

Changes whether the Server is in nonblocking mode. NOTE: It is strongly encouraged to use the websocket::async module instead of this. It provides high level APIs for creating asynchronous servers.

If it is in nonblocking mode, accept() will return an error instead of blocking when there are no incoming connections.

 // Suppose we have to work in a single thread, but want to
 // accomplish two unrelated things:
 // (1) Once in a while we want to check if anybody tried to connect to
 // our websocket server, and if so, handle the TcpStream.
 // (2) In between we need to something else, possibly unrelated to networking.

 let mut server = Server::bind("127.0.0.1:0").unwrap();

 // Set the server to non-blocking.
 server.set_nonblocking(true);

 for i in 1..3 {
    let result = match server.accept() {
        Ok(wsupgrade) => {
            // Do something with the established TcpStream.
        }
        _ => {
            // Nobody tried to connect, move on.
        }
    };
    // Perform another task. Because we have a non-blocking server,
    // this will execute independent of whether someone tried to
    // establish a connection.
    let two = 1+1;
 }

Turns an existing synchronous server into an asynchronous one. This will only work if the stream used for this server S already implements AsyncRead + AsyncWrite. Useful if you would like some blocking things to happen at the start of your server.

impl WsServer<TlsAcceptor, TcpListener>
[src]

Synchronous methods for creating an SSL server and accepting incoming connections.

Bind this Server to this socket, utilising the given SslContext

Secure Servers

extern crate websocket;
extern crate native_tls;
use std::thread;
use std::io::Read;
use std::fs::File;
use websocket::Message;
use websocket::sync::Server;
use native_tls::{Identity, TlsAcceptor};

// In this example we retrieve our keypair and certificate chain from a PKCS #12 archive,
// but but they can also be retrieved from, for example, individual PEM- or DER-formatted
// files. See the documentation for the `PKey` and `X509` types for more details.
let mut file = File::open("identity.pfx").unwrap();
let mut pkcs12 = vec![];
file.read_to_end(&mut pkcs12).unwrap();
let pkcs12 = Identity::from_pkcs12(&pkcs12, "hacktheplanet").unwrap();

let acceptor = TlsAcceptor::builder(pkcs12).build().unwrap();

let server = Server::bind_secure("127.0.0.1:1234", acceptor).unwrap();

for connection in server.filter_map(Result::ok) {
    // Spawn a new thread for each connection.
    thread::spawn(move || {
            let mut client = connection.accept().unwrap();

            let message = Message::text("Hello, client!");
            let _ = client.send_message(&message);

            // ...
    });
}

Wait for and accept an incoming WebSocket connection, returning a WebSocketRequest

impl WsServer<NoTlsAcceptor, TcpListener>
[src]

Bind this Server to this socket

Non-secure Servers

extern crate websocket;
use std::thread;
use websocket::Message;
use websocket::sync::Server;

let server = Server::bind("127.0.0.1:1234").unwrap();

for connection in server.filter_map(Result::ok) {
    // Spawn a new thread for each connection.
    thread::spawn(move || {
          let mut client = connection.accept().unwrap();

          let message = Message::text("Hello, client!");
          let _ = client.send_message(&message);

          // ...
   });
}

Wait for and accept an incoming WebSocket connection, returning a WebSocketRequest

Create a new independently owned handle to the underlying socket.

Trait Implementations

impl Iterator for WsServer<TlsAcceptor, TcpListener>
[src]

The type of the elements being iterated over.

Advances the iterator and returns the next value. Read more

Returns the bounds on the remaining length of the iterator. Read more

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Returns the nth element of the iterator. Read more

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Takes two iterators and creates a new iterator over both in sequence. Read more

'Zips up' two iterators into a single iterator of pairs. Read more

Takes a closure and creates an iterator which calls that closure on each element. Read more

Calls a closure on each element of an iterator. Read more

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Creates an iterator that both filters and maps. Read more

Creates an iterator which gives the current iteration count as well as the next value. Read more

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Creates an iterator that [skip]s elements based on a predicate. Read more

Creates an iterator that yields elements based on a predicate. Read more

Creates an iterator that skips the first n elements. Read more

Creates an iterator that yields its first n elements. Read more

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

Creates an iterator that works like map, but flattens nested structure. Read more

Creates an iterator that flattens nested structure. Read more

Creates an iterator which ends after the first [None]. Read more

Do something with each element of an iterator, passing the value on. Read more

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator method that applies a function as long as it returns successfully, producing a single, final value. Read more

An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error. Read more

An iterator method that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Applies function to the elements of iterator and returns the first non-none result. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Creates an iterator which [clone]s all of its elements. Read more

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more

impl Iterator for WsServer<NoTlsAcceptor, TcpListener>
[src]

The type of the elements being iterated over.

Advances the iterator and returns the next value. Read more

Returns the bounds on the remaining length of the iterator. Read more

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Returns the nth element of the iterator. Read more

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Takes two iterators and creates a new iterator over both in sequence. Read more

'Zips up' two iterators into a single iterator of pairs. Read more

Takes a closure and creates an iterator which calls that closure on each element. Read more

Calls a closure on each element of an iterator. Read more

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Creates an iterator that both filters and maps. Read more

Creates an iterator which gives the current iteration count as well as the next value. Read more

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Creates an iterator that [skip]s elements based on a predicate. Read more

Creates an iterator that yields elements based on a predicate. Read more

Creates an iterator that skips the first n elements. Read more

Creates an iterator that yields its first n elements. Read more

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

Creates an iterator that works like map, but flattens nested structure. Read more

Creates an iterator that flattens nested structure. Read more

Creates an iterator which ends after the first [None]. Read more

Do something with each element of an iterator, passing the value on. Read more

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator method that applies a function as long as it returns successfully, producing a single, final value. Read more

An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error. Read more

An iterator method that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Applies function to the elements of iterator and returns the first non-none result. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Creates an iterator which [clone]s all of its elements. Read more

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more

Auto Trait Implementations

impl<S, L> Send for WsServer<S, L> where
    L: Send,
    S: Send

impl<S, L> Sync for WsServer<S, L> where
    L: Sync,
    S: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> From for T
[src]

Performs the conversion.

impl<I> IntoIterator for I where
    I: Iterator
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> Typeable for T where
    T: Any
[src]

Get the TypeId of this object.

impl<I> UnicodeNormalization for I where
    I: Iterator<Item = char>, 
[src]

Returns an iterator over the string in Unicode Normalization Form D (canonical decomposition). Read more

Returns an iterator over the string in Unicode Normalization Form KD (compatibility decomposition). Read more

An Iterator over the string in Unicode Normalization Form C (canonical decomposition followed by canonical composition). Read more

An Iterator over the string in Unicode Normalization Form KC (compatibility decomposition followed by canonical composition). Read more

An Iterator over the string with Conjoining Grapheme Joiner characters inserted according to the Stream-Safe Text Process (UAX15-D4) Read more

impl<T> Erased for T
[src]