2018-04-13 09:18:42 +08:00
|
|
|
//! Simple websocket client.
|
|
|
|
use std::time::Duration;
|
2018-05-08 11:08:43 -07:00
|
|
|
use std::{io, thread};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
use actix::io::SinkWrite;
|
2018-04-13 09:18:42 +08:00
|
|
|
use actix::*;
|
2019-07-11 16:48:14 +08:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
use awc::{
|
|
|
|
error::WsProtocolError,
|
|
|
|
ws::{Codec, Frame, Message},
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
use futures::{
|
|
|
|
lazy,
|
|
|
|
stream::{SplitSink, Stream},
|
|
|
|
Future,
|
|
|
|
};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
2019-09-05 00:04:57 +09:00
|
|
|
env_logger::init();
|
2018-04-13 09:18:42 +08:00
|
|
|
let sys = actix::System::new("ws-example");
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
Arbiter::spawn(lazy(|| {
|
|
|
|
Client::new()
|
|
|
|
.ws("http://127.0.0.1:8080/ws/")
|
2018-04-13 09:18:42 +08:00
|
|
|
.connect()
|
|
|
|
.map_err(|e| {
|
|
|
|
println!("Error: {}", e);
|
|
|
|
})
|
2019-07-11 16:48:14 +08:00
|
|
|
.map(|(response, framed)| {
|
|
|
|
println!("{:?}", response);
|
|
|
|
let (sink, stream) = framed.split();
|
2018-07-16 12:36:53 +06:00
|
|
|
let addr = ChatClient::create(|ctx| {
|
2019-07-11 16:48:14 +08:00
|
|
|
ChatClient::add_stream(stream, ctx);
|
|
|
|
ChatClient(SinkWrite::new(sink, ctx))
|
2018-04-13 09:18:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// start console loop
|
2018-05-08 11:08:43 -07:00
|
|
|
thread::spawn(move || loop {
|
|
|
|
let mut cmd = String::new();
|
|
|
|
if io::stdin().read_line(&mut cmd).is_err() {
|
|
|
|
println!("error");
|
|
|
|
return;
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
2018-05-08 11:08:43 -07:00
|
|
|
addr.do_send(ClientCommand(cmd));
|
2018-04-13 09:18:42 +08:00
|
|
|
});
|
2019-07-11 16:48:14 +08:00
|
|
|
})
|
|
|
|
}));
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
struct ChatClient<T>(SinkWrite<SplitSink<Framed<T, Codec>>>)
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
struct ClientCommand(String);
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
impl<T: 'static> Actor for ChatClient<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
2018-04-13 09:18:42 +08:00
|
|
|
type Context = Context<Self>;
|
|
|
|
|
|
|
|
fn started(&mut self, ctx: &mut Context<Self>) {
|
|
|
|
// start heartbeats otherwise server will disconnect after 10 seconds
|
|
|
|
self.hb(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stopped(&mut self, _: &mut Context<Self>) {
|
|
|
|
println!("Disconnected");
|
|
|
|
|
|
|
|
// Stop application on disconnect
|
2018-07-16 12:36:53 +06:00
|
|
|
System::current().stop();
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
impl<T: 'static> ChatClient<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
2018-04-13 09:18:42 +08:00
|
|
|
fn hb(&self, ctx: &mut Context<Self>) {
|
|
|
|
ctx.run_later(Duration::new(1, 0), |act, ctx| {
|
2019-07-11 16:48:14 +08:00
|
|
|
act.0.write(Message::Ping(String::new())).unwrap();
|
2018-04-13 09:18:42 +08:00
|
|
|
act.hb(ctx);
|
2018-09-27 22:37:19 +03:00
|
|
|
|
|
|
|
// client should also check for a timeout here, similar to the
|
|
|
|
// server code
|
2018-04-13 09:18:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle stdin commands
|
2019-07-11 16:48:14 +08:00
|
|
|
impl<T: 'static> Handler<ClientCommand> for ChatClient<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
2018-04-13 09:18:42 +08:00
|
|
|
type Result = ();
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
fn handle(&mut self, msg: ClientCommand, _ctx: &mut Context<Self>) {
|
|
|
|
self.0.write(Message::Text(msg.0)).unwrap();
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle server websocket messages
|
2019-07-11 16:48:14 +08:00
|
|
|
impl<T: 'static> StreamHandler<Frame, WsProtocolError> for ChatClient<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
|
|
|
fn handle(&mut self, msg: Frame, _ctx: &mut Context<Self>) {
|
2019-09-05 00:04:57 +09:00
|
|
|
if let Frame::Text(txt) = msg {
|
|
|
|
println!("Server: {:?}", txt)
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:48:14 +08:00
|
|
|
fn started(&mut self, _ctx: &mut Context<Self>) {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("Connected");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finished(&mut self, ctx: &mut Context<Self>) {
|
|
|
|
println!("Server disconnected");
|
|
|
|
ctx.stop()
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 16:48:14 +08:00
|
|
|
|
|
|
|
impl<T: 'static> actix::io::WriteHandler<WsProtocolError> for ChatClient<T> where
|
|
|
|
T: AsyncRead + AsyncWrite
|
|
|
|
{
|
|
|
|
}
|