John Napiorkowski
/
PAGI-0.002005
/
examples/04-websocket-echo/README.md
04 – WebSocket Echo
Handles the WebSocket protocol:
- Waits for
websocket.connect.
- Sends
websocket.accept to complete the handshake.
- Echoes incoming frames back via
websocket.send.
- Stops when
websocket.disconnect arrives or when websocket.receive contains neither text nor bytes.
Quick Start
1. Start the server:
pagi-server --app examples/04-websocket-echo/app.pl --port 5000
2. Demo with websocat:
# Install websocat if needed
brew install websocat # macOS
# or: cargo install websocat # with Rust
# Connect and send messages
websocat ws://localhost:5000/
# Type: Hello
# => Hello
# Type: PAGI WebSocket!
# => PAGI WebSocket!
3. Or use JavaScript in browser console:
const ws = new WebSocket('ws://localhost:5000/');
ws.onmessage = (e) => console.log('Received:', e.data);
ws.onopen = () => ws.send('Hello from browser!');
Spec References
- WebSocket scope & events –
PAGI::Spec::Www