From b1061d0d233608f45c9bd215d563461f5b196a2f Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Mon, 13 Apr 2026 21:59:53 -0700 Subject: [PATCH] Correct and expand the WebSockets documentation Correct some errors that prevented the WebSockets sample code from compiling. Expand the testing instructions to confirm that the echo server is working as intended. --- docs/docs/controllers/websockets.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/docs/controllers/websockets.md b/docs/docs/controllers/websockets.md index 4aee7b68..611fbab0 100644 --- a/docs/docs/controllers/websockets.md +++ b/docs/docs/controllers/websockets.md @@ -28,7 +28,7 @@ The trait has two methods of interest: the first handles new WebSocket connectio incoming messages from the client. ```rust -use rwf::controller::Websocket; +use rwf::controller::WebsocketController; use rwf::prelude::*; #[derive(Default, macros::WebsocketController)] @@ -37,17 +37,17 @@ struct Echo; #[async_trait] impl WebsocketController for Echo { /// Run some code when a new client connects to the WebSocket server. - async fn handle_connection( + async fn client_connected( &self, client: &SessionId, ) -> Result<(), Error> { - log::info!("Client {:?} connected to the echo server", client); + println!("Client {:?} connected to the echo server", client); Ok(()) } /// Run some code when a client sends a message to the server. - async fn handle_message( + async fn client_message( &self, client: &SessionId, message: Message, @@ -113,7 +113,7 @@ use rwf::http::{Server, self}; #[tokio::main] async fn main() -> Result<(), http::Error> { - let server = Server::new(vec![ + Server::new(vec![ route!("/websocket" => Echo), ]) .launch() @@ -131,3 +131,12 @@ const ws = new WebSocket("ws://localhost:8000/websocket"); If everything works, you should see a log line in the terminal where the server is running, indicating a new client has joined the party. + +Now, send the server a message, with an event listener set up to print any messages received: + +```javascript +ws.onmessage = (event) => console.log(event.data); +ws.send("Hey there"); +``` + +You should see `Hey there` echoed back to you on the console.