mirror of
https://github.com/actix/examples
synced 2025-02-02 09:39:03 +01:00
feat: update ws client example to actix-web 1.0 (#143)
This commit is contained in:
parent
59df61db6f
commit
4aa663e794
@ -9,15 +9,17 @@ workspace = ".."
|
|||||||
name = "websocket-server"
|
name = "websocket-server"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
#[[bin]]
|
[[bin]]
|
||||||
#name = "websocket-client"
|
name = "websocket-client"
|
||||||
#path = "src/client.rs"
|
path = "src/client.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "0.8.2"
|
actix = "0.8.2"
|
||||||
|
actix-codec = "0.1.2"
|
||||||
actix-web = "1.0.0"
|
actix-web = "1.0.0"
|
||||||
actix-web-actors = "1.0.0"
|
actix-web-actors = "1.0.0"
|
||||||
actix-files = "0.1.1"
|
actix-files = "0.1.1"
|
||||||
|
awc = "0.2.1"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
@ -2,26 +2,39 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{io, thread};
|
use std::{io, thread};
|
||||||
|
|
||||||
|
use actix::io::SinkWrite;
|
||||||
use actix::*;
|
use actix::*;
|
||||||
use actix_web::client::{Client, ClientWriter, Message, ProtocolError};
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||||
use futures::Future;
|
use awc::{
|
||||||
|
error::WsProtocolError,
|
||||||
|
ws::{Codec, Frame, Message},
|
||||||
|
Client,
|
||||||
|
};
|
||||||
|
use futures::{
|
||||||
|
lazy,
|
||||||
|
stream::{SplitSink, Stream},
|
||||||
|
Future,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let sys = actix::System::new("ws-example");
|
let sys = actix::System::new("ws-example");
|
||||||
|
|
||||||
Arbiter::spawn(
|
Arbiter::spawn(lazy(|| {
|
||||||
Client::new("http://127.0.0.1:8080/ws/")
|
Client::new()
|
||||||
|
.ws("http://127.0.0.1:8080/ws/")
|
||||||
.connect()
|
.connect()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("Error: {}", e);
|
println!("Error: {}", e);
|
||||||
()
|
()
|
||||||
})
|
})
|
||||||
.map(|(reader, writer)| {
|
.map(|(response, framed)| {
|
||||||
|
println!("{:?}", response);
|
||||||
|
let (sink, stream) = framed.split();
|
||||||
let addr = ChatClient::create(|ctx| {
|
let addr = ChatClient::create(|ctx| {
|
||||||
ChatClient::add_stream(reader, ctx);
|
ChatClient::add_stream(stream, ctx);
|
||||||
ChatClient(writer)
|
ChatClient(SinkWrite::new(sink, ctx))
|
||||||
});
|
});
|
||||||
|
|
||||||
// start console loop
|
// start console loop
|
||||||
@ -35,18 +48,23 @@ fn main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
()
|
()
|
||||||
}),
|
})
|
||||||
);
|
}));
|
||||||
|
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ChatClient(ClientWriter);
|
struct ChatClient<T>(SinkWrite<SplitSink<Framed<T, Codec>>>)
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite;
|
||||||
|
|
||||||
#[derive(Message)]
|
#[derive(Message)]
|
||||||
struct ClientCommand(String);
|
struct ClientCommand(String);
|
||||||
|
|
||||||
impl Actor for ChatClient {
|
impl<T: 'static> Actor for ChatClient<T>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
type Context = Context<Self>;
|
type Context = Context<Self>;
|
||||||
|
|
||||||
fn started(&mut self, ctx: &mut Context<Self>) {
|
fn started(&mut self, ctx: &mut Context<Self>) {
|
||||||
@ -62,10 +80,13 @@ impl Actor for ChatClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatClient {
|
impl<T: 'static> ChatClient<T>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
fn hb(&self, ctx: &mut Context<Self>) {
|
fn hb(&self, ctx: &mut Context<Self>) {
|
||||||
ctx.run_later(Duration::new(1, 0), |act, ctx| {
|
ctx.run_later(Duration::new(1, 0), |act, ctx| {
|
||||||
act.0.ping("");
|
act.0.write(Message::Ping(String::new())).unwrap();
|
||||||
act.hb(ctx);
|
act.hb(ctx);
|
||||||
|
|
||||||
// client should also check for a timeout here, similar to the
|
// client should also check for a timeout here, similar to the
|
||||||
@ -75,24 +96,30 @@ impl ChatClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Handle stdin commands
|
/// Handle stdin commands
|
||||||
impl Handler<ClientCommand> for ChatClient {
|
impl<T: 'static> Handler<ClientCommand> for ChatClient<T>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
type Result = ();
|
type Result = ();
|
||||||
|
|
||||||
fn handle(&mut self, msg: ClientCommand, ctx: &mut Context<Self>) {
|
fn handle(&mut self, msg: ClientCommand, _ctx: &mut Context<Self>) {
|
||||||
self.0.text(msg.0)
|
self.0.write(Message::Text(msg.0)).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle server websocket messages
|
/// Handle server websocket messages
|
||||||
impl StreamHandler<Message, ProtocolError> for ChatClient {
|
impl<T: 'static> StreamHandler<Frame, WsProtocolError> for ChatClient<T>
|
||||||
fn handle(&mut self, msg: Message, ctx: &mut Context<Self>) {
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
|
fn handle(&mut self, msg: Frame, _ctx: &mut Context<Self>) {
|
||||||
match msg {
|
match msg {
|
||||||
Message::Text(txt) => println!("Server: {:?}", txt),
|
Frame::Text(txt) => println!("Server: {:?}", txt),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn started(&mut self, ctx: &mut Context<Self>) {
|
fn started(&mut self, _ctx: &mut Context<Self>) {
|
||||||
println!("Connected");
|
println!("Connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,3 +128,8 @@ impl StreamHandler<Message, ProtocolError> for ChatClient {
|
|||||||
ctx.stop()
|
ctx.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> actix::io::WriteHandler<WsProtocolError> for ChatClient<T> where
|
||||||
|
T: AsyncRead + AsyncWrite
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user