< 返回版块

adminSxs 发表于 2022-07-30 11:23

代码如下

struct Ws;

impl Ws{
 ...
}
impl Actor for Ws {
    type Context = ws::WebsocketContext<Self>;
}

impl StreamHandler<Result<Message, ProtocolError>> for Ws {
    fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
        match msg {
            Ok(Message::Ping(msg)) => ctx.pong(&msg),
            Ok(Message::Text(text)) => {
               
                let fut = async move {
                  //调用self.xxx方法
                };
                let _ = fut.into_actor(self).spawn(ctx);
            }
            Ok(Message::Close(reason)) => {
                ctx.stop();
                ctx.close(reason);
              
            }
            _ => (),
        }
    }
}


评论区

写评论
dlhxzb 2022-08-01 10:18

可以

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsConn {
    fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
        match msg {
            Ok(ws::Message::Ping(msg)) => {
                self.last_hb = Instant::now();
                ctx.pong(&msg);
            }
            Ok(ws::Message::Pong(_)) => {
                self.last_hb = Instant::now();
                self.send_hb(ctx);
            }
            Ok(ws::Message::Text(text)) => ctx.text(text),
            Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
            _ => (),
        }
    }
}
1 共 1 条评论, 1 页