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

pub struct Client<F, 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!");
client.send_message(&message).unwrap(); // Send message

Methods

impl Client<DataFrameSender<WebSocketStream>, Receiver<WebSocketStream>>
[src]

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

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

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().

fn shutdown_sender(&mut self) -> IoResult<()>

Shuts down the sending half of the client connection, will cause all pending and future IO to return immediately with an appropriate value.

fn shutdown_receiver(&mut self) -> IoResult<()>

Shuts down the receiving half of the client connection, will cause all pending and future IO to return immediately with an appropriate value.

fn shutdown(&mut self) -> IoResult<()>

Shuts down the client connection, will cause all pending and future IO to return immediately with an appropriate value.

impl<F: DataFrameable, S: Sender, R: Receiver<F>> Client<F, S, R>
[src]

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

Creates a Client from the given Sender and Receiver.

Essentially the opposite of Client.split().

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

Sends a single data frame to the remote endpoint.

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

Sends a single message to the remote endpoint.

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

Reads a single data frame from the remote endpoint.

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

Returns an iterator over incoming data frames.

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

Reads a single message from this receiver.

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

Returns an iterator over incoming messages.

use websocket::{Client, Message};

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

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

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() {
    let message: Message = message.unwrap();
    // Echo the message back
    sender.send_message(&message).unwrap();
}

fn get_sender(&self) -> &S

Returns a reference to the underlying Sender.

fn get_receiver(&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_receiver(&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() {
        let message: Message = message.unwrap();
        println!("Recv: {:?}", message);
    }
});

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