Actix Websocket Lobby会话数始终为1的问题排查求助
问题:WebSocket Lobby会话数始终为1,广播无法覆盖多连接
我参考WebSocket Lobby示例实现了广播功能,但遇到异常:无论打开多少个浏览器窗口/标签页建立连接,Lobby中的会话数始终显示为1,调用send_message_to_user发送广播消息时,也只有一个会话能接收。
核心代码
WebSocket连接初始化与广播接口
#[get("/ws")] pub async fn start_connection(req: HttpRequest, stream: Payload, lobby: Data<Addr<Lobby>>) -> Result<HttpResponse, Error> { let ws = WsConn::new( lobby.get_ref().clone()); let resp = ws::start(ws, &req, stream)?; Ok(resp) } #[get("/ws/send")] pub async fn send_message_to_user(lobby: Data<Addr<Lobby>>) ->impl Responder{ lobby.get_ref().send(BroadcastMessage { message: "Broadcast".to_string() }).await.unwrap(); HttpResponse::Ok().body("Message sent") }
复现步骤
- 打开项目
- 执行
cargo run启动Rust后端服务 - 执行
npm run dev启动React前端项目 - 访问
localhost:5173/ui - 查看Rust控制台输出,可见会话数始终维持为1
内容的提问来源于stack exchange,提问作者Samuel
相关产品推荐
相关产品推荐

