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

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.

[src]

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.

[src]

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.

[src]

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.

[src]

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.

[src]

Get the socket address of this server

[src]

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

[src]

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.

[src]

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::{Pkcs12, 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 = Pkcs12::from_der(&pkcs12, "hacktheplanet").unwrap();

let acceptor = TlsAcceptor::builder(pkcs12).unwrap().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);

            // ...
    });
}

[src]

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

impl WsServer<NoTlsAcceptor, TcpListener>
[src]

[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);

          // ...
   });
}

[src]

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

[src]

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.

[src]

Advances the iterator and returns the next value. Read more

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

Consumes the iterator, returning the last element. Read more

1.0.0
[src]

Returns the nth element of the iterator. Read more

Important traits for StepBy<I>
[src]

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

unstable replacement of Range::step_by

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

Important traits for Chain<A, B>
1.0.0
[src]

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

Important traits for Zip<A, B>
1.0.0
[src]

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

Important traits for Map<I, F>
1.0.0
[src]

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

1.21.0
[src]

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

Important traits for Filter<I, P>
1.0.0
[src]

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

Important traits for FilterMap<I, F>
1.0.0
[src]

Creates an iterator that both filters and maps. Read more

Important traits for Enumerate<I>
1.0.0
[src]

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

Important traits for Peekable<I>
1.0.0
[src]

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

Important traits for SkipWhile<I, P>
1.0.0
[src]

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

Important traits for TakeWhile<I, P>
1.0.0
[src]

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

Important traits for Skip<I>
1.0.0
[src]

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

Important traits for Take<I>
1.0.0
[src]

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

Important traits for Scan<I, St, F>
1.0.0
[src]

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

Important traits for FlatMap<I, U, F>
1.0.0
[src]

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

Important traits for Flatten<I>
[src]

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

Creates an iterator that flattens nested structure. Read more

Important traits for Fuse<I>
1.0.0
[src]

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

Important traits for Inspect<I, F>
1.0.0
[src]

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

Important traits for &'a mut W
1.0.0
[src]

Borrows an iterator, rather than consuming it. Read more

1.0.0
[src]

Transforms an iterator into a collection. Read more

1.0.0
[src]

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

[src]

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

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

[src]

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

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

Returns the maximum element of an iterator. Read more

1.0.0
[src]

Returns the minimum element of an iterator. Read more

1.6.0
[src]

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

1.15.0
[src]

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

1.6.0
[src]

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

1.15.0
[src]

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

Important traits for Rev<I>
1.0.0
[src]

Reverses an iterator's direction. Read more

1.0.0
[src]

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

Important traits for Cloned<I>
1.0.0
[src]

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

Important traits for Cycle<I>
1.0.0
[src]

Repeats an iterator endlessly. Read more

1.11.0
[src]

Sums the elements of an iterator. Read more

1.11.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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.

[src]

Advances the iterator and returns the next value. Read more

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

Consumes the iterator, returning the last element. Read more

1.0.0
[src]

Returns the nth element of the iterator. Read more

Important traits for StepBy<I>
[src]

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

unstable replacement of Range::step_by

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

Important traits for Chain<A, B>
1.0.0
[src]

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

Important traits for Zip<A, B>
1.0.0
[src]

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

Important traits for Map<I, F>
1.0.0
[src]

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

1.21.0
[src]

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

Important traits for Filter<I, P>
1.0.0
[src]

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

Important traits for FilterMap<I, F>
1.0.0
[src]

Creates an iterator that both filters and maps. Read more

Important traits for Enumerate<I>
1.0.0
[src]

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

Important traits for Peekable<I>
1.0.0
[src]

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

Important traits for SkipWhile<I, P>
1.0.0
[src]

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

Important traits for TakeWhile<I, P>
1.0.0
[src]

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

Important traits for Skip<I>
1.0.0
[src]

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

Important traits for Take<I>
1.0.0
[src]

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

Important traits for Scan<I, St, F>
1.0.0
[src]

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

Important traits for FlatMap<I, U, F>
1.0.0
[src]

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

Important traits for Flatten<I>
[src]

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

Creates an iterator that flattens nested structure. Read more

Important traits for Fuse<I>
1.0.0
[src]

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

Important traits for Inspect<I, F>
1.0.0
[src]

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

Important traits for &'a mut W
1.0.0
[src]

Borrows an iterator, rather than consuming it. Read more

1.0.0
[src]

Transforms an iterator into a collection. Read more

1.0.0
[src]

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

[src]

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

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

[src]

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

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

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

1.0.0
[src]

Returns the maximum element of an iterator. Read more

1.0.0
[src]

Returns the minimum element of an iterator. Read more

1.6.0
[src]

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

1.15.0
[src]

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

1.6.0
[src]

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

1.15.0
[src]

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

Important traits for Rev<I>
1.0.0
[src]

Reverses an iterator's direction. Read more

1.0.0
[src]

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

Important traits for Cloned<I>
1.0.0
[src]

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

Important traits for Cycle<I>
1.0.0
[src]

Repeats an iterator endlessly. Read more

1.11.0
[src]

Sums the elements of an iterator. Read more

1.11.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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

1.5.0
[src]

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