ws::start(WsEcho {}, &req, stream)
}
+struct WsRev;
+
+impl Actor for WsRev {
+ type Context = ws::WebsocketContext<Self>;
+}
+
+impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsRev {
+ fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
+ log::info!("WsRev got message {:?}", msg);
+ match msg {
+ Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
+ Ok(ws::Message::Text(text)) => {
+ let rev = text.chars().rev().collect::<String>();
+ ctx.text(rev);
+ },
+ Ok(ws::Message::Binary(bin)) => {
+ let mut rev = bin.to_vec();
+ rev.reverse();
+ ctx.binary(rev);
+ }
+ Ok(ws::Message::Close(reason)) => {
+ ctx.close(reason);
+ ctx.stop();
+ },
+ _ => ctx.stop(),
+ }
+ }
+}
+
+#[get("/ws-rev")]
+async fn ws_rev(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
+ ws::start(WsRev {}, &req, stream)
+}
+
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
.service(static_file)
.service(Files::new("/dir", "../static"))
.service(ws_echo)
+ .service(ws_rev)
.wrap(Logger::default())
})
.bind(("127.0.0.1", 3030))?
use futures_util::sink::SinkExt;
use futures_util::StreamExt;
- use super::{hello, static_file, ws_echo};
+ use super::{hello, static_file, ws_echo, ws_rev};
#[actix_web::test]
async fn test_hello() {
let received = client.next().await.unwrap().unwrap();
assert_eq!(received, ws::Frame::Binary(web::Bytes::from_static(&[42, 99])));
}
+
+ #[actix_web::test]
+ async fn test_ws_rev() {
+ // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn
+ let mut srv = actix_test::start(|| App::new().service(ws_rev));
+ let mut client = srv.ws_at("/ws-rev").await.unwrap();
+
+ // text reversed
+ client.send(ws::Message::Text("hello".into())).await.unwrap();
+ let received = client.next().await.unwrap().unwrap();
+ assert_eq!(received, ws::Frame::Text("olleh".into()));
+
+ // binary reversed
+ client.send(ws::Message::Binary(web::Bytes::from_static(&[42, 99]))).await.unwrap();
+ let received = client.next().await.unwrap().unwrap();
+ assert_eq!(received, ws::Frame::Binary(web::Bytes::from_static(&[99, 42])));
+ }
}