diff --git a/websocket/src/client.rs b/websocket/src/client.rs index e8cdaa19..3255193c 100644 --- a/websocket/src/client.rs +++ b/websocket/src/client.rs @@ -4,59 +4,58 @@ use std::{io, thread}; use actix::io::SinkWrite; use actix::*; -use actix_codec::{AsyncRead, AsyncWrite, Framed}; +use actix_codec::Framed; use awc::{ error::WsProtocolError, ws::{Codec, Frame, Message}, - Client, + BoxedSocket, Client, }; use bytes::Bytes; use futures::stream::{SplitSink, StreamExt}; -#[actix_rt::main] -async fn main() { +fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - let (response, framed) = Client::new() - .ws("http://127.0.0.1:8080/ws/") - .connect() - .await - .map_err(|e| { - println!("Error: {}", e); - }) - .unwrap(); + let sys = System::new("websocket-client"); - println!("{:?}", response); - let (sink, stream) = framed.split(); - let addr = ChatClient::create(|ctx| { - ChatClient::add_stream(stream, ctx); - ChatClient(SinkWrite::new(sink, ctx)) - }); + Arbiter::spawn(async { + let (response, framed) = Client::new() + .ws("http://127.0.0.1:8080/ws/") + .connect() + .await + .map_err(|e| { + println!("Error: {}", e); + }) + .unwrap(); - // start console loop - thread::spawn(move || loop { - let mut cmd = String::new(); - if io::stdin().read_line(&mut cmd).is_err() { - println!("error"); - return; - } - addr.do_send(ClientCommand(cmd)); + println!("{:?}", response); + let (sink, stream) = framed.split(); + let addr = ChatClient::create(|ctx| { + ChatClient::add_stream(stream, ctx); + ChatClient(SinkWrite::new(sink, ctx)) + }); + + // start console loop + thread::spawn(move || loop { + let mut cmd = String::new(); + if io::stdin().read_line(&mut cmd).is_err() { + println!("error"); + return; + } + addr.do_send(ClientCommand(cmd)); + }); }); + sys.run().unwrap(); } -struct ChatClient(SinkWrite, Message>>) -where - T: AsyncRead + AsyncWrite; +struct ChatClient(SinkWrite, Message>>); #[derive(Message)] #[rtype(result = "()")] struct ClientCommand(String); -impl Actor for ChatClient -where - T: AsyncRead + AsyncWrite, -{ +impl Actor for ChatClient { type Context = Context; fn started(&mut self, ctx: &mut Context) { @@ -72,10 +71,7 @@ where } } -impl ChatClient -where - T: AsyncRead + AsyncWrite, -{ +impl ChatClient { fn hb(&self, ctx: &mut Context) { ctx.run_later(Duration::new(1, 0), |act, ctx| { act.0.write(Message::Ping(Bytes::from_static(b""))).unwrap(); @@ -88,10 +84,7 @@ where } /// Handle stdin commands -impl Handler for ChatClient -where - T: AsyncRead + AsyncWrite, -{ +impl Handler for ChatClient { type Result = (); fn handle(&mut self, msg: ClientCommand, _ctx: &mut Context) { @@ -100,10 +93,7 @@ where } /// Handle server websocket messages -impl StreamHandler> for ChatClient -where - T: AsyncRead + AsyncWrite, -{ +impl StreamHandler> for ChatClient { fn handle(&mut self, msg: Result, _: &mut Context) { if let Ok(Frame::Text(txt)) = msg { println!("Server: {:?}", txt) @@ -120,7 +110,4 @@ where } } -impl actix::io::WriteHandler for ChatClient where - T: AsyncRead + AsyncWrite -{ -} +impl actix::io::WriteHandler for ChatClient {}