1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

update more examples and rustfmt

This commit is contained in:
Nikolay Kim
2019-03-09 18:03:09 -08:00
parent f39a53ea3a
commit e2945b9b39
48 changed files with 485 additions and 801 deletions

View File

@ -65,7 +65,9 @@ impl Encoder for ChatCodec {
type Error = io::Error;
fn encode(
&mut self, msg: ChatResponse, dst: &mut BytesMut,
&mut self,
msg: ChatResponse,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
let msg = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref();
@ -108,7 +110,9 @@ impl Encoder for ClientChatCodec {
type Error = io::Error;
fn encode(
&mut self, msg: ChatRequest, dst: &mut BytesMut,
&mut self,
msg: ChatRequest,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
let msg = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref();

View File

@ -19,7 +19,7 @@ extern crate actix_web;
use actix::*;
use actix_web::server::HttpServer;
use actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse};
use std::time::{Instant, Duration};
use std::time::{Duration, Instant};
mod codec;
mod server;
@ -191,7 +191,7 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession {
ws::Message::Binary(bin) => println!("Unexpected binary"),
ws::Message::Close(_) => {
ctx.stop();
},
}
}
}
}
@ -208,9 +208,7 @@ impl WsChatSession {
println!("Websocket Client heartbeat failed, disconnecting!");
// notify chat server
ctx.state()
.addr
.do_send(server::Disconnect { id: act.id });
ctx.state().addr.do_send(server::Disconnect { id: act.id });
// stop actor
ctx.stop();
@ -247,18 +245,21 @@ fn main() {
App::with_state(state)
// redirect to websocket.html
.resource("/", |r| r.method(http::Method::GET).f(|_| {
HttpResponse::Found()
.header("LOCATION", "/static/websocket.html")
.finish()
}))
.resource("/", |r| {
r.method(http::Method::GET).f(|_| {
HttpResponse::Found()
.header("LOCATION", "/static/websocket.html")
.finish()
})
})
// websocket
.resource("/ws/", |r| r.route().f(chat_route))
// static resources
.handler("/static/", fs::StaticFiles::new("static/").unwrap())
}).bind("127.0.0.1:8080")
.unwrap()
.start();
})
.bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();