View full source code or view the compiled example online
This example connects to an echo server on wss://echo.websocket.org, sends a ping message, and receives the response.
Cargo.tomlThe Cargo.toml enables features necessary to create a WebSocket object and to access events such as MessageEvent or ErrorEvent.
{{#include ../../../examples/websockets/Cargo.toml}}
src/lib.rsThis code shows the basic steps required to work with a WebSocket. At first it opens the connection, then subscribes to events onmessage, onerror, onopen. After the socket is opened it sends a ping message, receives an echoed response and prints it to the browser console.
{{#include ../../../examples/websockets/src/lib.rs}}