From 75b2835710d1ae99682b8d50c2c30afcd366b3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20=C5=9EURK?= Date: Sat, 12 Feb 2022 15:27:37 +0300 Subject: [PATCH] added another example. Added Router, request, close listener, and sec-websocket-key examples. All of these may be important when developing a real-world application for: 1. Identifying the socket connection of the client 2. Keeping an up-to-date list of connected sockets. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index a482357..ef2a2b0 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,35 @@ void main() { }); } ``` + + +To reach request object and disconnect event: +```dart + import 'package:shelf_router/shelf_router.dart'; + + var app = Router(); + app.get("/ws", FutureOr handler(Request request) { + return webSocketHandler((WebSocketChannel webSocket) { + // now we have access to request argument + + // that key is being generated by the websocket itself, every connection has a unique key. + final webSocketKey = request.headers["sec-websocket-key"]; + + webSocket.stream.listen((message) { + webSocket.sink.add("echo $message"); + }).onDone(() { + // client disconnected. + }); + webSocket.sink.add("the first message from server."); + })(request); + }); + await io.serve(app, 'localhost', 8080); +``` + + + + + + + +