mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 02:19:22 +02:00
websocket support
This commit is contained in:
62
src/main.rs
62
src/main.rs
@ -1,4 +1,4 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(dead_code, unused_variables)]
|
||||
extern crate actix;
|
||||
extern crate actix_http;
|
||||
extern crate tokio_core;
|
||||
@ -25,9 +25,9 @@ impl Route for MyRoute {
|
||||
{
|
||||
if let Some(pl) = payload {
|
||||
ctx.add_stream(pl);
|
||||
Self::http_stream(MyRoute{req: Some(req)})
|
||||
HttpMessage::stream(MyRoute{req: Some(req)})
|
||||
} else {
|
||||
Self::http_reply(req, httpcodes::HTTPOk)
|
||||
HttpMessage::reply_with(req, httpcodes::HTTPOk)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -45,7 +45,7 @@ impl Handler<PayloadItem> for MyRoute {
|
||||
{
|
||||
println!("CHUNK: {:?}", msg);
|
||||
if let Some(req) = self.req.take() {
|
||||
ctx.start(httpcodes::HTTPOk.into_response(req));
|
||||
ctx.start(httpcodes::HTTPOk.response(req));
|
||||
ctx.write_eof();
|
||||
}
|
||||
|
||||
@ -53,6 +53,57 @@ impl Handler<PayloadItem> for MyRoute {
|
||||
}
|
||||
}
|
||||
|
||||
struct MyWS {}
|
||||
|
||||
impl Actor for MyWS {
|
||||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
impl Route for MyWS {
|
||||
type State = ();
|
||||
|
||||
fn request(req: HttpRequest,
|
||||
payload: Option<Payload>,
|
||||
ctx: &mut HttpContext<Self>) -> HttpMessage<Self>
|
||||
{
|
||||
if let Some(payload) = payload {
|
||||
match ws::do_handshake(req) {
|
||||
Ok(resp) => {
|
||||
ctx.start(resp);
|
||||
ctx.add_stream(ws::WsStream::new(payload));
|
||||
HttpMessage::stream(MyWS{})
|
||||
},
|
||||
Err(err) =>
|
||||
HttpMessage::reply(err)
|
||||
}
|
||||
} else {
|
||||
HttpMessage::reply_with(req, httpcodes::HTTPBadRequest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ws::Message> for MyWS {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWS {}
|
||||
|
||||
impl Handler<ws::Message> for MyWS {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
-> Response<Self, ws::Message>
|
||||
{
|
||||
println!("WS: {:?}", msg);
|
||||
match msg {
|
||||
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, msg),
|
||||
ws::Message::Text(text) => ws::WsWriter::text(ctx, text),
|
||||
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
|
||||
_ => (),
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
@ -71,6 +122,9 @@ fn main() {
|
||||
routes.add_resource("/test")
|
||||
.post::<MyRoute>();
|
||||
|
||||
routes.add_resource("/ws/")
|
||||
.get::<MyWS>();
|
||||
|
||||
let http = HttpServer::new(routes);
|
||||
http.serve::<()>(
|
||||
&net::SocketAddr::from_str("127.0.0.1:9080").unwrap()).unwrap();
|
||||
|
Reference in New Issue
Block a user