You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:57:10