Skip to content

Commit cbedf0b

Browse files
committed
added websockets example to the guide
1 parent e16bb9c commit cbedf0b

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

examples/websockets/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
wasm-bindgen = "0.2.44"
11+
wasm-bindgen = "0.2.45"
1212

1313
[dependencies.web-sys]
14-
version = "0.3.21"
14+
version = "0.3.22"
1515
features = [
1616
"ErrorEvent",
1717
"MessageEvent",

examples/websockets/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,3 @@ http
2121
# or use python
2222
python -m SimpleHTTPServer
2323
```
24-
25-
Make sure that you have `WebSockets` server running

examples/websockets/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
</head>
88
<bodt>
99
</bodt>
10-
</html>
10+
</html>

examples/websockets/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@ extern "C" {
1414

1515
#[wasm_bindgen(start)]
1616
pub fn start_websocket() -> Result<(), JsValue> {
17-
// Assuming, you run a WebSockets server at localhost:8081
18-
// You'll also need to disable CORS in case of serving this example from
19-
// the different `host:port` then your WebSockets server
20-
let ws = WebSocket::new("ws://localhost:8081")
21-
.expect("should create a socket");
17+
// Connect to an echo server
18+
let ws = WebSocket::new("wss://echo.websocket.org")?;
2219

2320
// create callback
2421
let onmessage_callback = Closure::wrap(Box::new(move |e: MessageEvent| {
2522
// handle message
26-
console_log!("message event, received data {:?}", e.data());
23+
let response = e
24+
.data()
25+
.as_string()
26+
.expect("Can't convert received data to a string");
27+
console_log!("message event, received data: {:?}", response);
2728
}) as Box<dyn FnMut(MessageEvent)>);
2829
// set message event handler on WebSocket
2930
ws.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
3031
// forget the callback to keep it alive
3132
onmessage_callback.forget();
3233

3334
let onerror_callback = Closure::wrap(Box::new(move |e: ErrorEvent| {
34-
console_log!("error event {:?}", e);
35+
console_log!("error event: {:?}", e);
3536
}) as Box<dyn FnMut(ErrorEvent)>);
3637
ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
3738
onerror_callback.forget();
@@ -40,8 +41,8 @@ pub fn start_websocket() -> Result<(), JsValue> {
4041
let onopen_callback = Closure::wrap(Box::new(move |_| {
4142
console_log!("socket opened");
4243
match cloned_ws.send_with_str("ping") {
43-
Ok(_) => console_log!("message sent successfully"),
44-
Err(err) => console_log!("error sending message {:?}", err),
44+
Ok(_) => console_log!("message successfully sent"),
45+
Err(err) => console_log!("error sending message: {:?}", err),
4546
}
4647
}) as Box<dyn FnMut(JsValue)>);
4748
ws.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));

guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [web-sys: `canvas` Julia set](./examples/julia.md)
2222
- [web-sys: WebAudio](./examples/web-audio.md)
2323
- [web-sys: WebGL](./examples/webgl.md)
24+
- [web-sys: WebSockets](./examples/websockets.md)
2425
- [web-sys: `requestAnimationFrame`](./examples/request-animation-frame.md)
2526
- [web-sys: A Simple Paint Program](./examples/paint.md)
2627
- [Parallel Raytracing](./examples/raytrace.md)

guide/src/examples/websockets.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# WebSockets Example
2+
3+
[View full source code][code] or [view the compiled example online][online]
4+
5+
[online]: https://rustwasm.github.io/wasm-bindgen/exbuild/websockets/
6+
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/websockets/
7+
8+
This example connects to an echo server on `wss://echo.websocket.org`,
9+
sends a `ping` message, and receives the response.
10+
11+
## `Cargo.toml`
12+
13+
The `Cargo.toml` enables features necessary to create a `WebSocket` object and
14+
to access events such as `MessageEvent` or `ErrorEvent`.
15+
16+
```toml
17+
{{#include ../../../examples/websockets/Cargo.toml}}
18+
```
19+
20+
## `src/lib.rs`
21+
22+
This code shows the basic steps required to work with a `WebSocket`.
23+
At first it opens the connection, then subscribes to events `onmessage`, `onerror`, `onopen`.
24+
After the socket is opened it sends a `ping` message, receives an echoed response
25+
and prints it to the browser console.
26+
27+
```rust
28+
{{#include ../../../examples/websockets/src/lib.rs}}
29+
```

0 commit comments

Comments
 (0)