Struct websocket::server::Server [] [src]

pub struct Server<S> where S: OptionalSslAcceptor { /* 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.

Non-secure Servers

extern crate websocket;
use std::thread;
use websocket::{Server, Message};

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

           // ...
    });
}

Secure Servers

extern crate websocket;
extern crate openssl;
use std::thread;
use std::io::Read;
use std::fs::File;
use websocket::{Server, Message};
use openssl::pkcs12::Pkcs12;
use openssl::ssl::{SslMethod, SslAcceptorBuilder, SslStream};

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).unwrap();
let identity = pkcs12.parse("password123").unwrap();

let acceptor = SslAcceptorBuilder::mozilla_intermediate(SslMethod::tls(),
                                                        &identity.pkey,
                                                        &identity.cert,
                                                        &identity.chain)
                   .unwrap()
                   .build();

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

           // ...
    });
}

# 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::from_hyper 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 for more.

Methods

impl<S> Server<S> where S: OptionalSslAcceptor
[src]

Get the socket address of this server

Create a new independently owned handle to the underlying socket.

impl Server<SslAcceptor>
[src]

Bind this Server to this socket, utilising the given SslContext

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

Changes whether the Server is in nonblocking mode.

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

impl Server<NoSslAcceptor>
[src]

Bind this Server to this socket

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

Trait Implementations

impl Iterator for Server<SslAcceptor>
[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

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

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 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 adaptor 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

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 Server<NoSslAcceptor>
[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

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

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 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 adaptor 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

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