mirror of
https://github.com/actix/examples
synced 2025-06-29 02:10:36 +02:00
convert more examples
This commit is contained in:
@ -1,72 +1,43 @@
|
||||
#[macro_use]
|
||||
extern crate actix;
|
||||
extern crate byteorder;
|
||||
extern crate bytes;
|
||||
extern crate futures;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_tcp;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use actix::prelude::*;
|
||||
use futures::Future;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use std::{io, net, process, thread};
|
||||
use tokio_codec::FramedRead;
|
||||
use tokio_io::io::WriteHalf;
|
||||
use tokio_io::AsyncRead;
|
||||
use tokio_tcp::TcpStream;
|
||||
use std::{io, net, thread};
|
||||
use tokio::io::{split, WriteHalf};
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_util::codec::FramedRead;
|
||||
|
||||
mod codec;
|
||||
|
||||
fn main() {
|
||||
let sys = actix::System::new("chat-client");
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() {
|
||||
// Connect to server
|
||||
let addr = net::SocketAddr::from_str("127.0.0.1:12345").unwrap();
|
||||
Arbiter::spawn(
|
||||
TcpStream::connect(&addr)
|
||||
.and_then(|stream| {
|
||||
let addr = ChatClient::create(|ctx| {
|
||||
let (r, w) = stream.split();
|
||||
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));
|
||||
});
|
||||
|
||||
futures::future::ok(())
|
||||
})
|
||||
.map_err(|e| {
|
||||
println!("Can not connect to server: {}", e);
|
||||
process::exit(1)
|
||||
}),
|
||||
);
|
||||
|
||||
println!("Running chat client");
|
||||
sys.run();
|
||||
|
||||
let stream = TcpStream::connect(&addr).await.unwrap();
|
||||
|
||||
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 {
|
||||
@ -74,6 +45,7 @@ struct ChatClient {
|
||||
}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "()")]
|
||||
struct ClientCommand(String);
|
||||
|
||||
impl Actor for ChatClient {
|
||||
@ -140,23 +112,27 @@ impl Handler<ClientCommand> for ChatClient {
|
||||
|
||||
/// Server communication
|
||||
|
||||
impl StreamHandler<codec::ChatResponse, io::Error> for ChatClient {
|
||||
fn handle(&mut self, msg: codec::ChatResponse, _: &mut Context<Self>) {
|
||||
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 {
|
||||
codec::ChatResponse::Message(ref msg) => {
|
||||
Ok(codec::ChatResponse::Message(ref msg)) => {
|
||||
println!("message: {}", msg);
|
||||
}
|
||||
codec::ChatResponse::Joined(ref msg) => {
|
||||
Ok(codec::ChatResponse::Joined(ref msg)) => {
|
||||
println!("!!! joined: {}", msg);
|
||||
}
|
||||
codec::ChatResponse::Rooms(rooms) => {
|
||||
Ok(codec::ChatResponse::Rooms(rooms)) => {
|
||||
println!("\n!!! Available rooms:");
|
||||
for room in rooms {
|
||||
println!("{}", room);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
_ => (),
|
||||
_ => ctx.stop(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user