1
0
mirror of https://github.com/actix/examples synced 2025-02-13 05:52:20 +01:00

140 lines
3.7 KiB
Rust
Raw Normal View History

2018-05-08 11:08:43 -07:00
use actix::prelude::*;
use std::str::FromStr;
use std::time::Duration;
2019-12-16 13:09:54 +06:00
use std::{io, net, thread};
use tokio::io::{split, WriteHalf};
use tokio::net::TcpStream;
use tokio_util::codec::FramedRead;
mod codec;
2020-09-12 16:49:45 +01:00
#[actix_web::main]
2019-12-16 13:09:54 +06:00
async fn main() {
// Connect to server
let addr = net::SocketAddr::from_str("127.0.0.1:12345").unwrap();
2018-05-08 11:08:43 -07:00
2019-12-16 13:09:54 +06:00
println!("Running chat client");
2019-12-16 13:09:54 +06:00
let stream = TcpStream::connect(&addr).await.unwrap();
2019-12-16 13:09:54 +06:00
let addr = ChatClient::create(|ctx| {
let (r, w) = split(stream);
ChatClient::add_stream(FramedRead::new(r, codec::ClientChatCodec), ctx);
ChatClient {
framed: actix::io::FramedWrite::new(w, codec::ClientChatCodec, 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));
});
}
struct ChatClient {
2020-09-12 16:49:45 +01:00
framed: actix::io::FramedWrite<
codec::ChatRequest,
WriteHalf<TcpStream>,
codec::ClientChatCodec,
>,
}
#[derive(Message)]
2019-12-16 13:09:54 +06:00
#[rtype(result = "()")]
struct ClientCommand(String);
impl Actor for ChatClient {
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();
}
}
impl ChatClient {
fn hb(&self, ctx: &mut Context<Self>) {
ctx.run_later(Duration::new(1, 0), |act, ctx| {
act.framed.write(codec::ChatRequest::Ping);
act.hb(ctx);
2018-09-27 22:37:19 +03:00
// client should also check for a timeout here, similar to the
// server code
});
}
}
impl actix::io::WriteHandler<io::Error> for ChatClient {}
/// Handle stdin commands
impl Handler<ClientCommand> for ChatClient {
type Result = ();
fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) {
let m = msg.0.trim();
if m.is_empty() {
2018-05-08 11:08:43 -07:00
return;
}
// we check for /sss type of messages
if m.starts_with('/') {
let v: Vec<&str> = m.splitn(2, ' ').collect();
match v[0] {
"/list" => {
self.framed.write(codec::ChatRequest::List);
2018-05-08 11:08:43 -07:00
}
"/join" => {
if v.len() == 2 {
2018-05-20 21:03:29 -07:00
self.framed.write(codec::ChatRequest::Join(v[1].to_owned()));
} else {
println!("!!! room name is required");
}
2018-05-08 11:08:43 -07:00
}
_ => println!("!!! unknown command"),
}
} else {
2018-05-20 21:03:29 -07:00
self.framed.write(codec::ChatRequest::Message(m.to_owned()));
}
}
}
/// Server communication
2019-12-16 13:09:54 +06:00
impl StreamHandler<Result<codec::ChatResponse, io::Error>> for ChatClient {
fn handle(
&mut self,
msg: Result<codec::ChatResponse, io::Error>,
ctx: &mut Context<Self>,
) {
match msg {
2019-12-16 13:09:54 +06:00
Ok(codec::ChatResponse::Message(ref msg)) => {
println!("message: {}", msg);
}
2019-12-16 13:09:54 +06:00
Ok(codec::ChatResponse::Joined(ref msg)) => {
println!("!!! joined: {}", msg);
}
2019-12-16 13:09:54 +06:00
Ok(codec::ChatResponse::Rooms(rooms)) => {
println!("\n!!! Available rooms:");
for room in rooms {
println!("{}", room);
}
2019-09-05 00:04:57 +09:00
println!();
}
2019-12-16 13:09:54 +06:00
_ => ctx.stop(),
}
}
}