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
|
extern crate env_logger;
#[cfg(feature = "ssl")]
extern crate openssl;
extern crate url;
extern crate ws;
#[cfg(feature = "ssl")]
use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};
#[cfg(feature = "ssl")]
use ws::util::TcpStream;
#[cfg(feature = "ssl")]
struct Client {
out: ws::Sender,
}
#[cfg(feature = "ssl")]
impl ws::Handler for Client {
fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
println!("msg = {}", msg);
self.out.close(ws::CloseCode::Normal)
}
fn upgrade_ssl_client(
&mut self,
sock: TcpStream,
_: &url::Url,
) -> ws::Result<SslStream<TcpStream>> {
let mut builder = SslConnector::builder(SslMethod::tls()).map_err(|e| {
ws::Error::new(
ws::ErrorKind::Internal,
format!("Failed to upgrade client to SSL: {}", e),
)
})?;
builder.set_verify(SslVerifyMode::empty());
let connector = builder.build();
connector
.configure()
.unwrap()
.use_server_name_indication(false)
.verify_hostname(false)
.connect("", sock)
.map_err(From::from)
}
}
#[cfg(feature = "ssl")]
fn main() {
// Setup logging
env_logger::init();
if let Err(error) = ws::connect("wss://localhost:3443/api/websocket", |out| {
if let Err(_) = out.send("Hello WebSocket") {
println!("Websocket couldn't queue an initial message.")
} else {
println!("Client sent message 'Hello WebSocket'. ")
}
Client { out }
}) {
println!("Failed to create WebSocket due to: {:?}", error);
}
}
#[cfg(not(feature = "ssl"))]
fn main() {
println!("SSL feature is not enabled.")
}
|