mirror of
https://github.com/actix/actix-web.git
synced 2024-11-22 17:41:11 +00:00
use threads for websocket example
This commit is contained in:
parent
0bfe07b371
commit
0865071dd7
3 changed files with 23 additions and 4 deletions
|
@ -17,6 +17,7 @@ Actix web is licensed under the [Apache-2.0 license](http://opensource.org/licen
|
|||
* Keep-alive and slow requests support
|
||||
* [WebSockets support](https://fafhrd91.github.io/actix-web/actix_web/ws/index.html)
|
||||
* [Configurable request routing](https://fafhrd91.github.io/actix-web/actix_web/struct.RoutingMap.html)
|
||||
* Multipart streams
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -29,6 +30,11 @@ actix-web = { git = "https://github.com/fafhrd91/actix-web.git" }
|
|||
|
||||
## Example
|
||||
|
||||
* [Mulitpart support](https://github.com/fafhrd91/actix-web/tree/master/examples/multipart)
|
||||
* [Simple websocket example](https://github.com/fafhrd91/actix-web/tree/master/examples/websocket)
|
||||
* [Tcp/Websocket chat](https://github.com/fafhrd91/actix-web/tree/master/examples/websocket-chat)
|
||||
|
||||
|
||||
```rust
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
This is extension of the
|
||||
[actix chat example](https://github.com/fafhrd91/actix/tree/master/examples/chat)
|
||||
|
||||
Added features:
|
||||
|
||||
* Browser WebSocket client
|
||||
* Chat server runs in separate thread
|
||||
* Tcp listener runs in separate thread
|
||||
|
||||
|
||||
## Server
|
||||
|
||||
|
@ -10,6 +16,7 @@ Chat server listens for incoming tcp connections. Server can access several type
|
|||
|
||||
* `\list` - list all available rooms
|
||||
* `\join name` - join room, if room does not exist, create new one
|
||||
* `\name name` - set session name
|
||||
* `some message` - just string, send messsage to all peers in same room
|
||||
* client has to send heartbeat `Ping` messages, if server does not receive a heartbeat
|
||||
message for 10 seconds connection gets droppped
|
||||
|
|
|
@ -204,11 +204,17 @@ fn main() {
|
|||
let _ = env_logger::init();
|
||||
let sys = actix::System::new("websocket-example");
|
||||
|
||||
// Start chat server actor
|
||||
let server: SyncAddress<_> = server::ChatServer::default().start();
|
||||
// Start chat server actor in separate thread
|
||||
let server: SyncAddress<_> =
|
||||
Arbiter::start(|_| server::ChatServer::default());
|
||||
|
||||
// Start tcp server
|
||||
session::TcpServer::new("127.0.0.1:12345", server.clone());
|
||||
// Start tcp server in separate thread
|
||||
let srv = server.clone();
|
||||
Arbiter::new("tcp-server").send::<msgs::Execute>(
|
||||
msgs::Execute::new(move || {
|
||||
session::TcpServer::new("127.0.0.1:12345", srv);
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
// Websocket sessions state
|
||||
let state = WsChatSessionState { addr: server };
|
||||
|
|
Loading…
Reference in a new issue