This commit is contained in:
jonnnh 2019-07-19 11:05:17 +08:00
parent 1de46e05c1
commit 9fe2597518

View file

@ -2,14 +2,14 @@ extern crate lemmy_server;
#[macro_use] #[macro_use]
extern crate diesel_migrations; extern crate diesel_migrations;
use std::time::{Instant, Duration};
use std::env;
use actix_web::*;
use actix::prelude::*; use actix::prelude::*;
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_web::*;
use actix_web_actors::ws; use actix_web_actors::ws;
use lemmy_server::websocket::server::*;
use lemmy_server::db::establish_connection; use lemmy_server::db::establish_connection;
use lemmy_server::websocket::server::*;
use std::env;
use std::time::{Duration, Instant};
embed_migrations!(); embed_migrations!();
@ -18,15 +18,19 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
/// How long before lack of client response causes a timeout /// How long before lack of client response causes a timeout
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
/// Entry point for our route /// Entry point for our route
fn chat_route(req: HttpRequest, stream: web::Payload, chat_server: web::Data<Addr<ChatServer>>) -> Result<HttpResponse, Error> { fn chat_route(
req: HttpRequest,
stream: web::Payload,
chat_server: web::Data<Addr<ChatServer>>,
) -> Result<HttpResponse, Error> {
ws::start( ws::start(
WSSession { WSSession {
cs_addr: chat_server.get_ref().to_owned(), cs_addr: chat_server.get_ref().to_owned(),
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
ip: req.connection_info() ip: req
.connection_info()
.remote() .remote()
.unwrap_or("127.0.0.1:12345") .unwrap_or("127.0.0.1:12345")
.split(":") .split(":")
@ -155,8 +159,10 @@ impl WSSession {
println!("Websocket Client heartbeat failed, disconnecting!"); println!("Websocket Client heartbeat failed, disconnecting!");
// notify chat server // notify chat server
act.cs_addr act.cs_addr.do_send(Disconnect {
.do_send(Disconnect { id: act.id, ip: act.ip.to_owned() }); id: act.id,
ip: act.ip.to_owned(),
});
// stop actor // stop actor
ctx.stop(); ctx.stop();
@ -182,15 +188,15 @@ fn main() {
let server = ChatServer::default().start(); let server = ChatServer::default().start();
// Create Http server with websocket support // Create Http server with websocket support
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.data(server.clone()) .data(server.clone())
.service(web::resource("/api/v1/ws").to(chat_route)) .service(web::resource("/api/v1/ws").to(chat_route))
// .service(web::resource("/api/v1/rest").route(web::post().to(||{}))) // .service(web::resource("/api/v1/rest").route(web::post().to(||{})))
.service(web::resource("/").to(index)) .service(web::resource("/").to(index))
// static resources // static resources
.service(actix_files::Files::new("/static", front_end_dir())) .service(actix_files::Files::new("/static", front_end_dir()))
}).bind("0.0.0.0:8536") })
.bind("0.0.0.0:8536")
.unwrap() .unwrap()
.start(); .start();