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

pub struct Client<F, S, R> { /* 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<DataFrame, Sender<WebSocketStream>, Receiver<WebSocketStream>>
[src]

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

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

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

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

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]

Creates a Client from the given Sender and Receiver.

Essentially the opposite of Client.split().

Sends a single data frame to the remote endpoint.

Sends a single message to the remote endpoint.

Reads a single data frame from the remote endpoint.

Returns an iterator over incoming data frames.

Reads a single message from this receiver.

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

Returns a reference to the underlying Sender.

Returns a reference to the underlying Receiver.

Returns a mutable reference to the underlying Sender.

Returns a mutable reference to the underlying Receiver.

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