1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-04 03:51:55 +01:00
actix-extras/examples/websocket-chat/src/client.rs

154 lines
4.3 KiB
Rust
Raw Normal View History

2018-01-05 23:01:19 +01:00
#[macro_use] extern crate actix;
2017-10-21 02:16:17 +02:00
extern crate bytes;
extern crate byteorder;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
use std::{io, net, process, thread};
use std::str::FromStr;
use std::time::Duration;
use futures::Future;
2018-01-28 10:04:58 +01:00
use tokio_io::AsyncRead;
2018-02-08 23:03:27 +01:00
use tokio_io::io::WriteHalf;
use tokio_io::codec::FramedRead;
2017-10-21 02:16:17 +02:00
use tokio_core::net::TcpStream;
use actix::prelude::*;
mod codec;
fn main() {
let sys = actix::System::new("chat-client");
// Connect to server
let addr = net::SocketAddr::from_str("127.0.0.1:12345").unwrap();
Arbiter::handle().spawn(
TcpStream::connect(&addr, Arbiter::handle())
.and_then(|stream| {
2018-02-13 02:42:10 +01:00
let addr: Addr<Syn, _> = ChatClient::create(|ctx| {
2018-02-08 23:03:27 +01:00
let (r, w) = stream.split();
ChatClient::add_stream(FramedRead::new(r, codec::ClientChatCodec), ctx);
ChatClient{
framed: actix::io::FramedWrite::new(
w, codec::ClientChatCodec, ctx)}});
2017-10-21 02:16:17 +02:00
// start console loop
thread::spawn(move|| {
loop {
let mut cmd = String::new();
if io::stdin().read_line(&mut cmd).is_err() {
println!("error");
return
}
2018-02-13 08:13:06 +01:00
addr.do_send(ClientCommand(cmd));
2017-10-21 02:16:17 +02:00
}
});
futures::future::ok(())
})
.map_err(|e| {
println!("Can not connect to server: {}", e);
process::exit(1)
})
);
println!("Running chat client");
sys.run();
}
2018-01-28 10:04:58 +01:00
struct ChatClient {
2018-02-08 23:03:27 +01:00
framed: actix::io::FramedWrite<WriteHalf<TcpStream>, codec::ClientChatCodec>,
2018-01-28 10:04:58 +01:00
}
2017-10-21 02:16:17 +02:00
2018-01-05 23:01:19 +01:00
#[derive(Message)]
2017-10-21 02:16:17 +02:00
struct ClientCommand(String);
impl Actor for ChatClient {
2018-01-28 10:04:58 +01:00
type Context = Context<Self>;
2017-10-21 02:16:17 +02:00
2018-01-28 10:04:58 +01:00
fn started(&mut self, ctx: &mut Context<Self>) {
2017-10-21 02:16:17 +02:00
// start heartbeats otherwise server will disconnect after 10 seconds
self.hb(ctx)
}
2018-01-05 23:01:19 +01:00
2018-02-15 22:59:25 +01:00
fn stopped(&mut self, _: &mut Context<Self>) {
2018-01-05 23:01:19 +01:00
println!("Disconnected");
// Stop application on disconnect
2018-02-13 08:13:06 +01:00
Arbiter::system().do_send(actix::msgs::SystemExit(0));
2018-01-05 23:01:19 +01:00
}
2017-10-21 02:16:17 +02:00
}
impl ChatClient {
2018-01-28 10:04:58 +01:00
fn hb(&self, ctx: &mut Context<Self>) {
2017-10-21 02:16:17 +02:00
ctx.run_later(Duration::new(1, 0), |act, ctx| {
2018-02-08 23:03:27 +01:00
act.framed.write(codec::ChatRequest::Ping);
2018-01-28 10:04:58 +01:00
act.hb(ctx);
2017-10-21 02:16:17 +02:00
});
}
}
2018-02-08 23:03:27 +01:00
impl actix::io::WriteHandler<io::Error> for ChatClient {}
2017-10-21 02:16:17 +02:00
/// Handle stdin commands
2018-01-05 23:01:19 +01:00
impl Handler<ClientCommand> for ChatClient {
type Result = ();
2018-01-28 10:04:58 +01:00
fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) {
2017-10-21 02:16:17 +02:00
let m = msg.0.trim();
if m.is_empty() {
2018-01-05 23:01:19 +01:00
return
2017-10-21 02:16:17 +02:00
}
// we check for /sss type of messages
if m.starts_with('/') {
let v: Vec<&str> = m.splitn(2, ' ').collect();
match v[0] {
"/list" => {
2018-02-08 23:03:27 +01:00
self.framed.write(codec::ChatRequest::List);
2017-10-21 02:16:17 +02:00
},
"/join" => {
if v.len() == 2 {
2018-02-08 23:03:27 +01:00
self.framed.write(codec::ChatRequest::Join(v[1].to_owned()));
2017-10-21 02:16:17 +02:00
} else {
println!("!!! room name is required");
}
},
_ => println!("!!! unknown command"),
}
} else {
2018-02-08 23:03:27 +01:00
self.framed.write(codec::ChatRequest::Message(m.to_owned()));
2017-10-21 02:16:17 +02:00
}
}
}
/// Server communication
2018-02-08 23:03:27 +01:00
impl StreamHandler<codec::ChatResponse, io::Error> for ChatClient {
2017-10-21 02:16:17 +02:00
2018-02-03 17:25:31 +01:00
fn handle(&mut self, msg: codec::ChatResponse, _: &mut Context<Self>) {
2017-10-21 02:16:17 +02:00
match msg {
2018-02-03 17:25:31 +01:00
codec::ChatResponse::Message(ref msg) => {
println!("message: {}", msg);
}
codec::ChatResponse::Joined(ref msg) => {
println!("!!! joined: {}", msg);
}
codec::ChatResponse::Rooms(rooms) => {
println!("\n!!! Available rooms:");
for room in rooms {
println!("{}", room);
2018-01-05 23:01:19 +01:00
}
2018-02-03 17:25:31 +01:00
println!("");
2017-10-21 02:16:17 +02:00
}
2018-02-03 17:25:31 +01:00
_ => (),
2017-10-21 02:16:17 +02:00
}
}
}