mirror of
https://github.com/actix/examples
synced 2025-02-17 15:23:31 +01:00
migrate websocket example
This commit is contained in:
parent
d633a35fdd
commit
1779f963d9
@ -2,19 +2,22 @@
|
|||||||
name = "websocket"
|
name = "websocket"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
workspace = "../"
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
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]
|
||||||
env_logger = "*"
|
actix = { git="https://github.com/actix/actix.git" }
|
||||||
|
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
actix-web-actors = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||||
|
env_logger = "0.6"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
|
bytes = "0.4"
|
||||||
actix = "0.7"
|
|
||||||
actix-web = "0.7"
|
|
@ -3,17 +3,16 @@
|
|||||||
//! or [python console client](https://github.com/actix/examples/blob/master/websocket/websocket-client.py)
|
//! or [python console client](https://github.com/actix/examples/blob/master/websocket/websocket-client.py)
|
||||||
//! could be used for testing.
|
//! could be used for testing.
|
||||||
|
|
||||||
#![allow(unused_variables)]
|
|
||||||
extern crate actix;
|
|
||||||
extern crate actix_web;
|
|
||||||
extern crate env_logger;
|
|
||||||
|
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
|
use actix_files as fs;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse,
|
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
|
use actix_web_actors::ws;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::Stream;
|
||||||
|
|
||||||
/// How often heartbeat pings are sent
|
/// How often heartbeat pings are sent
|
||||||
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
||||||
@ -21,8 +20,14 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
|||||||
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
|
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||||
|
|
||||||
/// do websocket handshake and start `MyWebSocket` actor
|
/// do websocket handshake and start `MyWebSocket` actor
|
||||||
fn ws_index(r: &HttpRequest) -> Result<HttpResponse, Error> {
|
fn ws_index<S>(r: HttpRequest, stream: web::Payload<S>) -> Result<HttpResponse, Error>
|
||||||
ws::start(r, MyWebSocket::new())
|
where
|
||||||
|
S: Stream<Item = Bytes, Error = error::PayloadError> + 'static,
|
||||||
|
{
|
||||||
|
println!("{:?}", r);
|
||||||
|
let res = ws::start(MyWebSocket::new(), &r, stream);
|
||||||
|
println!("{:?}", res.as_ref().unwrap());
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
/// websocket connection is long running connection, it easier
|
/// websocket connection is long running connection, it easier
|
||||||
@ -91,30 +96,20 @@ impl MyWebSocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("ws-example");
|
|
||||||
|
|
||||||
server::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// enable logger
|
// enable logger
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
// websocket route
|
// websocket route
|
||||||
.resource("/ws/", |r| r.method(http::Method::GET).f(ws_index))
|
.service(web::resource("/ws/").route(web::get().to(ws_index)))
|
||||||
// static files
|
// static files
|
||||||
.handler(
|
.service(fs::Files::new("/", "static/").index_file("index.html"))
|
||||||
"/",
|
|
||||||
fs::StaticFiles::new("static/")
|
|
||||||
.unwrap()
|
|
||||||
.index_file("index.html"),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
// start http server on 127.0.0.1:8080
|
// start http server on 127.0.0.1:8080
|
||||||
.bind("127.0.0.1:8080")
|
.bind("127.0.0.1:8080")?
|
||||||
.unwrap()
|
.run()
|
||||||
.start();
|
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8080");
|
|
||||||
let _ = sys.run();
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user