mirror of
https://github.com/actix/examples
synced 2025-01-22 14:05:55 +01:00
Add bare-bones WS server for autobahn testing (#331)
This commit is contained in:
parent
5b3c996774
commit
b7ff0d1ffe
@ -46,6 +46,7 @@ members = [
|
||||
"unix-socket",
|
||||
"web-cors/backend",
|
||||
"websocket",
|
||||
"websocket-autobahn",
|
||||
"websocket-chat",
|
||||
"websocket-chat-broker",
|
||||
"websocket-tcp-chat",
|
||||
|
16
websocket-autobahn/Cargo.toml
Normal file
16
websocket-autobahn/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "websocket-autobahn"
|
||||
version = "2.0.0"
|
||||
authors = ["Mark Lodato <mlodato517@gmail.com"]
|
||||
edition = "2018"
|
||||
|
||||
[[bin]]
|
||||
name = "websocket-autobahn-server"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
actix = "0.9.0"
|
||||
actix-web = "2.0.0"
|
||||
actix-web-actors = "2.0.0"
|
||||
actix-rt = "1.0.0"
|
||||
env_logger = "0.7"
|
33
websocket-autobahn/README.md
Normal file
33
websocket-autobahn/README.md
Normal file
@ -0,0 +1,33 @@
|
||||
# websocket
|
||||
|
||||
Websocket server for autobahn suite testing.
|
||||
|
||||
## Usage
|
||||
|
||||
### server
|
||||
|
||||
```bash
|
||||
cd examples/websocket-autobahn
|
||||
cargo run --bin websocket-autobahn-server
|
||||
```
|
||||
|
||||
### Running Autobahn Test Suite
|
||||
|
||||
Running the autobahn test suite is easiest using the docker image
|
||||
as explained on the [autobahn-testsuite repo](https://github.com/crossbario/autobahn-testsuite#using-the-testsuite-docker-image).
|
||||
|
||||
First, start a server (see above). Then, run the test suite in fuzzingclient mode:
|
||||
|
||||
```bash
|
||||
docker run -it --rm \
|
||||
-v "${PWD}/config:/config" \
|
||||
-v "${PWD}/reports:/reports" \
|
||||
--network host \
|
||||
--name autobahn \
|
||||
crossbario/autobahn-testsuite \
|
||||
wstest \
|
||||
--spec /config/fuzzingclient.json \
|
||||
--mode fuzzingclient
|
||||
```
|
||||
|
||||
Results are written to the `reports/servers` directory for viewing.
|
15
websocket-autobahn/config/fuzzingclient.json
Normal file
15
websocket-autobahn/config/fuzzingclient.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"outdir": "./reports/servers",
|
||||
"servers": [
|
||||
{
|
||||
"agent": "actix-web-actors",
|
||||
"url": "ws://host.docker.internal:9001"
|
||||
}
|
||||
],
|
||||
"cases": ["*"],
|
||||
"exclude-cases": [
|
||||
"12.*",
|
||||
"13.*"
|
||||
],
|
||||
"exclude-agent-cases": {}
|
||||
}
|
2
websocket-autobahn/reports/.gitignore
vendored
Normal file
2
websocket-autobahn/reports/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
55
websocket-autobahn/src/main.rs
Normal file
55
websocket-autobahn/src/main.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use actix::prelude::*;
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web_actors::ws;
|
||||
|
||||
async fn ws_index(r: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
ws::start(WebSocket::new(), &r, stream)
|
||||
}
|
||||
|
||||
struct WebSocket {}
|
||||
|
||||
impl Actor for WebSocket {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
|
||||
fn started(&mut self, _ctx: &mut Self::Context) {}
|
||||
}
|
||||
|
||||
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocket {
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: Result<ws::Message, ws::ProtocolError>,
|
||||
ctx: &mut Self::Context,
|
||||
) {
|
||||
if let Ok(msg) = msg {
|
||||
match msg {
|
||||
ws::Message::Text(text) => ctx.text(text),
|
||||
ws::Message::Binary(bin) => ctx.binary(bin),
|
||||
ws::Message::Ping(bytes) => ctx.pong(&bytes),
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
ctx.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WebSocket {
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(ws_index)))
|
||||
})
|
||||
.bind("127.0.0.1:9001")?
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user