1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/examples/websocket/src/client.rs

114 lines
2.7 KiB
Rust
Raw Normal View History

2018-01-28 07:03:03 +01:00
//! Simple websocket client.
#![allow(unused_variables)]
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
extern crate tokio_core;
use std::{io, thread};
use std::time::Duration;
use actix::*;
use futures::Future;
use actix_web::ws::{Message, ProtocolError, Client, ClientWriter};
2018-01-28 07:03:03 +01:00
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
let sys = actix::System::new("ws-example");
Arbiter::handle().spawn(
Client::new("http://127.0.0.1:8080/ws/")
2018-02-24 06:41:58 +01:00
.connect()
2018-01-28 07:03:03 +01:00
.map_err(|e| {
println!("Error: {}", e);
()
})
.map(|(reader, writer)| {
2018-02-13 04:15:39 +01:00
let addr: Addr<Syn, _> = ChatClient::create(|ctx| {
2018-01-28 17:58:18 +01:00
ChatClient::add_stream(reader, ctx);
2018-01-28 07:03:03 +01:00
ChatClient(writer)
});
// 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));
2018-01-28 07:03:03 +01:00
}
});
()
})
);
let _ = sys.run();
}
struct ChatClient(ClientWriter);
2018-01-28 07:03:03 +01:00
#[derive(Message)]
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)
}
2018-02-15 22:59:25 +01:00
fn stopped(&mut self, _: &mut Context<Self>) {
println!("Disconnected");
2018-01-28 07:03:03 +01:00
// Stop application on disconnect
2018-02-13 08:13:06 +01:00
Arbiter::system().do_send(actix::msgs::SystemExit(0));
2018-01-28 07:03:03 +01:00
}
}
impl ChatClient {
fn hb(&self, ctx: &mut Context<Self>) {
ctx.run_later(Duration::new(1, 0), |act, ctx| {
act.0.ping("");
act.hb(ctx);
});
}
}
/// Handle stdin commands
impl Handler<ClientCommand> for ChatClient {
type Result = ();
fn handle(&mut self, msg: ClientCommand, ctx: &mut Context<Self>) {
2018-03-04 19:44:41 +01:00
self.0.text(msg.0)
2018-01-28 07:03:03 +01:00
}
}
/// Handle server websocket messages
impl StreamHandler<Message, ProtocolError> for ChatClient {
2018-01-28 07:03:03 +01:00
2018-01-28 17:58:18 +01:00
fn handle(&mut self, msg: Message, ctx: &mut Context<Self>) {
2018-01-28 07:03:03 +01:00
match msg {
2018-01-28 17:58:18 +01:00
Message::Text(txt) => println!("Server: {:?}", txt),
2018-01-28 07:03:03 +01:00
_ => ()
}
}
2018-01-28 17:58:18 +01:00
2018-01-28 18:07:12 +01:00
fn started(&mut self, ctx: &mut Context<Self>) {
2018-01-28 17:58:18 +01:00
println!("Connected");
}
2018-01-31 22:18:30 +01:00
fn finished(&mut self, ctx: &mut Context<Self>) {
2018-01-28 17:58:18 +01:00
println!("Server disconnected");
ctx.stop()
}
2018-01-28 07:03:03 +01:00
}