Skip to content

Commit 4e84b6d

Browse files
authored
Correct and expand the WebSockets documentation (#79)
Get the WebSockets sample code to compile; expand the testing instructions
1 parent 4e0e53a commit 4e84b6d

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

docs/docs/controllers/websockets.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The trait has two methods of interest: the first handles new WebSocket connectio
2828
incoming messages from the client.
2929

3030
```rust
31-
use rwf::controller::Websocket;
31+
use rwf::controller::WebsocketController;
3232
use rwf::prelude::*;
3333

3434
#[derive(Default, macros::WebsocketController)]
@@ -37,17 +37,17 @@ struct Echo;
3737
#[async_trait]
3838
impl WebsocketController for Echo {
3939
/// Run some code when a new client connects to the WebSocket server.
40-
async fn handle_connection(
40+
async fn client_connected(
4141
&self,
4242
client: &SessionId,
4343
) -> Result<(), Error> {
44-
log::info!("Client {:?} connected to the echo server", client);
44+
println!("Client {:?} connected to the echo server", client);
4545

4646
Ok(())
4747
}
4848

4949
/// Run some code when a client sends a message to the server.
50-
async fn handle_message(
50+
async fn client_message(
5151
&self,
5252
client: &SessionId,
5353
message: Message,
@@ -113,7 +113,7 @@ use rwf::http::{Server, self};
113113

114114
#[tokio::main]
115115
async fn main() -> Result<(), http::Error> {
116-
let server = Server::new(vec![
116+
Server::new(vec![
117117
route!("/websocket" => Echo),
118118
])
119119
.launch()
@@ -131,3 +131,12 @@ const ws = new WebSocket("ws://localhost:8000/websocket");
131131

132132
If everything works, you should see a log line in the terminal where the server is running, indicating a new
133133
client has joined the party.
134+
135+
Now, send the server a message, with an event listener set up to print any messages received:
136+
137+
```javascript
138+
ws.onmessage = (event) => console.log(event.data);
139+
ws.send("Hey there");
140+
```
141+
142+
You should see `Hey there` echoed back to you on the console.

0 commit comments

Comments
 (0)