-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathclient.rs
70 lines (60 loc) · 2.3 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// The MIT License (MIT)
// Copyright (c) 2019 David Haig
// Demo websocket client connecting to localhost port 1337.
// This will initiate a websocket connection to path /chat. The demo sends a simple "Hello, World!"
// message and expects an echo of the same message as a reply.
// It will then initiate a close handshake, wait for a close response from the server,
// and terminate the connection.
// Note that we are using the standard library in the demo and making use of the framer helper module
// but the websocket library remains no_std (see client_full for an example without the framer helper module)
use embedded_websocket::{
framer::{Framer, FramerError, ReadResult},
WebSocketClient, WebSocketCloseStatusCode, WebSocketOptions, WebSocketSendMessageType,
};
use std::{error::Error, net::TcpStream};
fn main() -> Result<(), FramerError<impl Error>> {
// open a TCP stream to localhost port 1337
let address = "127.0.0.1:1337";
println!("Connecting to: {}", address);
let mut stream = TcpStream::connect(address).map_err(FramerError::Io)?;
println!("Connected.");
let mut read_buf = [0; 4000];
let mut read_cursor = 0;
let mut write_buf = [0; 4000];
let mut frame_buf = [0; 4000];
let mut websocket = WebSocketClient::new_client(rand::thread_rng());
// initiate a websocket opening handshake
let websocket_options = WebSocketOptions {
path: "/chat",
host: "localhost",
origin: "http://localhost:1337",
sub_protocols: None,
additional_headers: None,
};
let mut framer = Framer::new(
&mut read_buf,
&mut read_cursor,
&mut write_buf,
&mut websocket,
);
framer.connect(&mut stream, &websocket_options)?;
let message = "Hello, World!";
framer.write(
&mut stream,
WebSocketSendMessageType::Text,
true,
message.as_bytes(),
)?;
while let ReadResult::Text(s) = framer.read(&mut stream, &mut frame_buf)? {
println!("Received: {}", s);
// close the websocket after receiving the first reply
framer.close(
&mut stream,
WebSocketCloseStatusCode::NormalClosure,
Some("Done chatting"),
)?;
println!("Sent close handshake");
}
println!("Connection closed");
Ok(())
}