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]

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

[src]

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

[src]

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

[src]

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

[src]

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]

[src]

Creates a Client from the given Sender and Receiver.

Essentially the opposite of Client.split().

[src]

Sends a single data frame to the remote endpoint.

[src]

Sends a single message to the remote endpoint.

[src]

Reads a single data frame from the remote endpoint.

[src]

Returns an iterator over incoming data frames.

[src]

Reads a single message from this receiver.

[src]

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

[src]

Returns a reference to the underlying Sender.

[src]

Returns a reference to the underlying Receiver.

[src]

Returns a mutable reference to the underlying Sender.

[src]

Returns a mutable reference to the underlying Receiver.

[src]

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