-
I followed the example and wrote a WebSocket, It keeps prompting that the type is incorrect. I don't quite understand the reason for this error. Actor use std::collections::HashMap;
use actix::{Actor, Context, Message, Handler, Recipient };
use uuid::Uuid;
#[derive(Message)]
#[rtype(result = "()")]
pub struct WsMessage(pub String);
#[derive(Message)]
#[rtype(usize)]
pub struct Connect {
pub id: Uuid,
pub addr: Recipient<WsMessage>,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct Disconnect {
pub id: Uuid,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct LogMessage {
pub id: i64,
pub text: String
}
pub struct WsActor {
sessions: HashMap<Uuid, Recipient<WsMessage>>,
}
impl WsActor {
pub fn new() -> WsActor {
WsActor {
sessions: HashMap::new(),
}
}
}
impl Actor for WsActor {
type Context = Context<Self>;
}
impl Handler<Connect> for WsActor {
type Result = usize;
fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
// self.session.insert(msg.id, msg.addr);
0
}
}
impl Handler<Disconnect> for WsActor {
type Result = ();
fn handle(&mut self, msg: Disconnect, _ctx: &mut Context<Self>) -> Self::Result {
// self.session.remove(&msg.id);
}
}
impl Handler<LogMessage> for WsActor {
type Result = ();
fn handle(&mut self, msg: LogMessage, _ctx: &mut Context<Self>) -> Self::Result {
}
} use actix::{fut, Actor, ActorContext, ActorFutureExt, Addr, Running, StreamHandler, WrapFuture, ContextFutureSpawner, AsyncContext};
use actix_web::{
get,
web::{self, Data},
Error, HttpRequest, HttpResponse,
};
use actix_web_actors::ws;
use uuid::Uuid;
use crate::actors::ws_actor::{Connect, WsActor, Disconnect};
pub struct WebsocketConnect {
ws_actor: Addr<WsActor>,
}
impl WebsocketConnect {
pub fn new(ws_actor: Addr<WsActor>) -> WebsocketConnect {
WebsocketConnect {
ws_actor,
}
}
}
impl Actor for WebsocketConnect {
type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
let addr = ctx.address();
self.ws_actor
.send(Connect {
id: Uuid::new_v4(),
addr: addr.recipient(),
})
.into_actor(self)
.then(|res, _, ctx| {
match res {
Ok(_res) => (),
_ => ctx.stop(),
}
fut::ready(())
})
.wait(ctx);
}
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
let addr = ctx.address();
self.ws_actor.do_send(Disconnect {
id: Uuid::new_v4()
});
Running::Stop
}
}
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebsocketConnect {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
match msg {
_ => (),
}
}
}
#[get("/ws/")]
pub async fn handler_ws(
req: HttpRequest,
stream: web::Payload,
srv: Data<Addr<WsActor>>,
) -> Result<HttpResponse, Error> {
let resp = ws::start(
WebsocketConnect::new(srv.get_ref().clone()),
&req,
stream,
);
resp
} |
Beta Was this translation helpful? Give feedback.
Answered by
robjtede
Jan 31, 2023
Replies: 1 comment 1 reply
-
Seems like your versions of actix are incompatible. Run |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
robothot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like your versions of actix are incompatible. Run
cargo tree -i actix
to figure out what's happening.