I am learning Actix and try creating a WebSocket service
code snippets:
starts server
pub async fn start(addr: &str) -> std::result::Result<(), IoError> {
let connections = Connections::default().start();
HttpServer::new(move || {
App::new().service(
web::resource("/ws/")
.data(connections.clone())
.route(web::get().to(ws_index)),
)
})
.bind(addr)?
.run()
.await
}
handler
async fn ws_index(
req: HttpRequest,
stream: web::Payload,
addr: web::Data<Addr<Connections>>,
) -> Result<HttpResponse, Error> {
let id = Uuid::new_v4().to_simple().to_string();
let client = Connection::new(id, addr.get_ref().clone());
let resp = ws::start(client, &req, stream);
resp
}
streamhandler
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Connection {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
Ok(ws::Message::Pong(_)) => self.hb = Instant::now(),
Ok(ws::Message::Text(text)) => ctx.text(text),
Ok(ws::Message::Close(reason)) => {
ctx.stop();
println!("Connection {} closed with reason {:?}", self.id, reason);
}
Err(e) => println!("Error: {}", e),
_ => (),
}
}
}
Now the WebSocket server is running, it can receive a text and send it back. But if I send a large text, the server logs "Error: A payload reached size limit.". How to fix it?