MCPcopy Create free account
hub / github.com/actix/examples / echo_ws

Function echo_ws

websockets/echo/src/main.rs:17–31  ·  view source on GitHub ↗

WebSocket handshake and start `MyWebSocket` actor.

(req: HttpRequest, stream: web::Payload)

Source from the content-addressed store, hash-verified

15
16/// WebSocket handshake and start `MyWebSocket` actor.
17async fn echo_ws(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
18 let (res, session, stream) = actix_ws::handle(&req, stream)?;
19
20 let (actor, _handle) = Actor::spawn(None, MyWebSocket, session).await.unwrap();
21
22 actix_web::rt::spawn(async move {
23 let mut stream = stream.aggregate_continuations();
24
25 while let Some(Ok(msg)) = stream.recv().await {
26 actor.send_message(WsMessage::Ws(msg)).unwrap();
27 }
28 });
29
30 Ok(res)
31}
32
33// the actor-based WebSocket examples REQUIRE `actix_web::main` for actor support
34#[actix_web::main]

Callers

nothing calls this directly

Calls 1

send_messageMethod · 0.45

Tested by

no test coverage detected