Update examples to compile again

pull/93/head
Sebastian Dröge 5 years ago
parent 07d4721ffd
commit f659af4493
  1. 14
      examples/callback-error.rs
  2. 4
      examples/client.rs
  3. 21
      examples/server.rs

@ -2,19 +2,19 @@ use std::net::TcpListener;
use std::thread::spawn; use std::thread::spawn;
use tungstenite::accept_hdr; use tungstenite::accept_hdr;
use tungstenite::handshake::server::{ErrorResponse, Request}; use tungstenite::handshake::server::{Request, Response};
use tungstenite::http::StatusCode; use tungstenite::http::StatusCode;
fn main() { fn main() {
let server = TcpListener::bind("127.0.0.1:3012").unwrap(); let server = TcpListener::bind("127.0.0.1:3012").unwrap();
for stream in server.incoming() { for stream in server.incoming() {
spawn(move || { spawn(move || {
let callback = |_req: &Request| { let callback = |_req: &Request, _resp| {
Err(ErrorResponse { let resp = Response::builder()
error_code: StatusCode::FORBIDDEN, .status(StatusCode::FORBIDDEN)
headers: None, .body(Some("Access denied".into()))
body: Some("Access denied".into()), .unwrap();
}) Err(resp)
}; };
accept_hdr(stream.unwrap(), callback).unwrap_err(); accept_hdr(stream.unwrap(), callback).unwrap_err();
}); });

@ -8,9 +8,9 @@ fn main() {
connect(Url::parse("ws://localhost:3012/socket").unwrap()).expect("Can't connect"); connect(Url::parse("ws://localhost:3012/socket").unwrap()).expect("Can't connect");
println!("Connected to the server"); println!("Connected to the server");
println!("Response HTTP code: {}", response.code); println!("Response HTTP code: {}", response.status());
println!("Response contains the following headers:"); println!("Response contains the following headers:");
for &(ref header, _ /*value*/) in response.headers.iter() { for (ref header, _value) in response.headers() {
println!("* {}", header); println!("* {}", header);
} }

@ -2,30 +2,27 @@ use std::net::TcpListener;
use std::thread::spawn; use std::thread::spawn;
use tungstenite::accept_hdr; use tungstenite::accept_hdr;
use tungstenite::handshake::server::Request; use tungstenite::handshake::server::{Request, Response};
fn main() { fn main() {
env_logger::init(); env_logger::init();
let server = TcpListener::bind("127.0.0.1:3012").unwrap(); let server = TcpListener::bind("127.0.0.1:3012").unwrap();
for stream in server.incoming() { for stream in server.incoming() {
spawn(move || { spawn(move || {
let callback = |req: &Request| { let callback = |req: &Request, mut response: Response| {
println!("Received a new ws handshake"); println!("Received a new ws handshake");
println!("The request's path is: {}", req.path); println!("The request's path is: {}", req.uri().path());
println!("The request's headers are:"); println!("The request's headers are:");
for &(ref header, _ /* value */) in req.headers.iter() { for (ref header, _value) in req.headers() {
println!("* {}", header); println!("* {}", header);
} }
// Let's add an additional header to our response to the client. // Let's add an additional header to our response to the client.
let extra_headers = vec![ let headers = response.headers_mut();
(String::from("MyCustomHeader"), String::from(":)")), headers.append("MyCustomHeader", ":)".parse().unwrap());
( headers.append("SOME_TUNGSTENITE_HEADER", "header_value".parse().unwrap());
String::from("SOME_TUNGSTENITE_HEADER"),
String::from("header_value"), Ok(response)
),
];
Ok(Some(extra_headers))
}; };
let mut websocket = accept_hdr(stream.unwrap(), callback).unwrap(); let mut websocket = accept_hdr(stream.unwrap(), callback).unwrap();

Loading…
Cancel
Save