Struct websocket::client::Client [-] [+] [src]

pub struct Client<D, S, R> {
    // some fields omitted
}

Represents a WebSocket client, which can send and receive messages/data frames.

D is the data frame type, S is the type implementing Sender<D> and R is the type implementing Receiver<D>.

For most cases, the data frame type will be dataframe::DataFrame, the Sender type will be client::Sender<stream::WebSocketStream> and the receiver type will be client::Receiver<stream::WebSocketStream>.

A Client can be split into a Sender and a Receiver which can then be moved to different threads, often using a send loop and receiver loop concurrently, as shown in the client example in examples/client.rs.

Connecting to a Server

extern crate websocket;

use websocket::{Client, Message};
use websocket::client::request::Url;

let url = Url::parse("ws://127.0.0.1:1234").unwrap(); // Get the URL
let request = Client::connect(url).unwrap(); // Connect to the server
let response = request.send().unwrap(); // Send the request
response.validate().unwrap(); // Ensure the response is valid

let mut client = response.begin(); // Get a Client

let message = Message::Text("Hello, World!".to_string());
client.send_message(message).unwrap(); // Send message

Methods

impl Client<DataFrame, Sender<WebSocketStream>, Receiver<WebSocketStream>>

fn connect<T: ToWebSocketUrlComponents>(components: T) -> WebSocketResult<Request<WebSocketStream, WebSocketStream>>

Connects to the given ws:// or wss:// URL and return a Request to be sent.

A connection is established, however the request is not sent to the server until a call to send().

fn connect_ssl_context<T: ToWebSocketUrlComponents>(components: T, context: &SslContext) -> WebSocketResult<Request<WebSocketStream, WebSocketStream>>

Connects to the specified wss:// URL using the given SSL context.

If a ws:// URL is supplied, a normal, non-secure connection is established and the context parameter is ignored.

A connection is established, however the request is not sent to the server until a call to send().

impl<D, S: Sender<D>, R: Receiver<D>> Client<D, S, R>

fn new(sender: S, receiver: R) -> Client<D, S, R>

Creates a Client from the given Sender and Receiver.

Essentially the opposite of Client.split().

fn send_dataframe(&mut self, dataframe: D) -> WebSocketResult<()>

Sends a single data frame to the remote endpoint.

fn send_message<M>(&mut self, message: M) -> WebSocketResult<()> where M: Message<D>

Sends a single message to the remote endpoint.

fn recv_dataframe(&mut self) -> WebSocketResult<D>

Reads a single data frame from the remote endpoint.

fn incoming_dataframes<'a>(&'a mut self) -> DataFrameIterator<'a, R, D>

Returns an iterator over incoming data frames.

fn recv_message<M, I>(&mut self) -> WebSocketResult<M> where M: Message<D, DataFrameIterator=I>, I: Iterator<Item=D>

Reads a single message from this receiver.

fn incoming_messages<'a, M>(&'a mut self) -> MessageIterator<'a, R, D, M> where M: Message<D>

Returns an iterator over incoming messages.

use websocket::{Client, Message};

let mut client = response.begin(); // Get a Client

for message in client.incoming_messages::<Message>() {
    println!("Recv: {:?}", message.unwrap());
}

Note that since this method mutably borrows the Client, it may be necessary to first split() the Client and call incoming_messages() on the returned Receiver to be able to send messages within an iteration.

use websocket::{Client, Message, Sender, Receiver};

let client = response.begin(); // Get a Client
let (mut sender, mut receiver) = client.split(); // Split the Client
for message in receiver.incoming_messages::<Message>() {
    // Echo the message back
    sender.send_message(message.unwrap()).unwrap();
}

fn get_sender(&self) -> &S

Returns a reference to the underlying Sender.

fn get_reciever(&self) -> &R

Returns a reference to the underlying Receiver.

fn get_mut_sender(&mut self) -> &mut S

Returns a mutable reference to the underlying Sender.

fn get_mut_reciever(&mut self) -> &mut R

Returns a mutable reference to the underlying Receiver.

fn split(self) -> (S, R)

Split this client into its constituent Sender and Receiver pair.

This allows the Sender and Receiver to be sent to different threads.

use websocket::{Client, Message, Sender, Receiver};
use std::thread;

let client = response.begin(); // Get a Client

let (mut sender, mut receiver) = client.split();

thread::spawn(move || {
    for message in receiver.incoming_messages::<Message>() {
        println!("Recv: {:?}", message.unwrap());
    }
});

let message = Message::Text("Hello, World!".to_string());
sender.send_message(message).unwrap();