Update README and examples

pull/18/head
Daniel Abramov 8 years ago
parent 44a15c9eab
commit 5982d4094d
  1. 8
      README.md
  2. 9
      examples/client.rs
  3. 5
      src/handshake/headers.rs

@ -8,15 +8,21 @@ Lightweight stream-based WebSocket implementation for [Rust](http://www.rust-lan
let server = TcpListener::bind("127.0.0.1:9001").unwrap(); let server = TcpListener::bind("127.0.0.1:9001").unwrap();
for stream in server.incoming() { for stream in server.incoming() {
spawn (move || { spawn (move || {
let mut websocket = accept(stream.unwrap()).unwrap(); let mut websocket = accept(stream.unwrap(), None).unwrap();
loop { loop {
let msg = websocket.read_message().unwrap(); let msg = websocket.read_message().unwrap();
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap(); websocket.write_message(msg).unwrap();
} }
}
}); });
} }
``` ```
Take a look at the examples section to see how to write a simple client/server.
[![MIT licensed](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE-MIT) [![MIT licensed](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE-MIT)
[![Apache-2.0 licensed](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE) [![Apache-2.0 licensed](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](./LICENSE-APACHE)
[![Crates.io](https://img.shields.io/crates/v/tungstenite.svg?maxAge=2592000)](https://crates.io/crates/tungstenite) [![Crates.io](https://img.shields.io/crates/v/tungstenite.svg?maxAge=2592000)](https://crates.io/crates/tungstenite)

@ -8,9 +8,16 @@ use tungstenite::{Message, connect};
fn main() { fn main() {
env_logger::init().unwrap(); env_logger::init().unwrap();
let (mut socket, _) = connect(Url::parse("ws://localhost:3012/socket").unwrap()) let (mut socket, response) = connect(Url::parse("ws://localhost:3012/socket").unwrap())
.expect("Can't connect"); .expect("Can't connect");
println!("Connected to the server");
println!("Response HTTP code: {}", response.code);
println!("Response contains the following headers:");
for &(ref header, _ /*value*/) in response.headers.iter() {
println!("* {}", header);
}
socket.write_message(Message::Text("Hello WebSocket".into())).unwrap(); socket.write_message(Message::Text("Hello WebSocket".into())).unwrap();
loop { loop {
let msg = socket.read_message().expect("Error reading message"); let msg = socket.read_message().expect("Error reading message");

@ -49,6 +49,11 @@ impl Headers {
.unwrap_or(false) .unwrap_or(false)
} }
/// Allows to iterate over available headers.
pub fn iter(&self) -> slice::Iter<(String, Box<[u8]>)> {
self.data.iter()
}
} }
/// The iterator over headers. /// The iterator over headers.

Loading…
Cancel
Save