ws function
MethodRouter
ws(
- WebSocketHandler handler, {
- Iterable<
String> ? protocols, - Duration? pingInterval,
Serves a WebSocket upgrade at this path.
The handshake is a GET, so this registers one, and the same router serves
it beside ordinary routes:
final app = Router()
..route('/chat/{room}', ws(joinRoom))
..route('/health', get(health));
A handler that throws closes the connection with
WebSocketClose.handlerFailed rather than leaving it open, and the error
reaches the router's onError.
Implementation
MethodRouter ws(
WebSocketHandler handler, {
Iterable<String>? protocols,
Duration? pingInterval,
}) {
return get(
(request) {
final upgrade = webSocketHandler(
(channel, protocol) async {
final session = WebSocketSession(
channel,
request,
negotiatedProtocol: protocol,
);
try {
await handler(session);
} on Object catch (error, stack) {
ServerErrors.report(error, stack);
await channel.sink
.close(WebSocketClose.handlerFailed, 'handler failed');
}
},
protocols: protocols,
pingInterval: pingInterval,
);
return upgrade(request);
},
);
}