OLD | NEW |
1 ## Web Socket Handler for Shelf | 1 ## Web Socket Handler for Shelf |
2 | 2 |
3 `shelf_web_socket` is a [Shelf][] handler for establishing [WebSocket][] | 3 `shelf_web_socket` is a [Shelf][] handler for establishing [WebSocket][] |
4 connections. It exposes a single function, [webSocketHandler][], which calls an | 4 connections. It exposes a single function, [webSocketHandler][], which calls an |
5 `onConnection` callback with a [CompatibleWebSocket][] object for every | 5 `onConnection` callback with a [CompatibleWebSocket][] object for every |
6 connection that's established. | 6 connection that's established. |
7 | 7 |
8 [Shelf]: pub.dartlang.org/packages/shelf | 8 [Shelf]: https://pub.dartlang.org/packages/shelf |
9 | 9 |
10 [WebSocket]: https://tools.ietf.org/html/rfc6455 | 10 [WebSocket]: https://tools.ietf.org/html/rfc6455 |
11 | 11 |
12 [webSocketHandler]: https://api.dartlang.org/apidocs/channels/be/dartdoc-viewer/
shelf_web_socket/shelf_web_socket.webSocketHandler | 12 [webSocketHandler]: https://api.dartlang.org/apidocs/channels/be/dartdoc-viewer/
shelf_web_socket/shelf_web_socket.webSocketHandler |
13 | 13 |
14 [CompatibleWebSocket]: https://api.dartlang.org/apidocs/channels/be/dartdoc-view
er/http_parser/http_parser.CompatibleWebSocket | 14 [CompatibleWebSocket]: https://api.dartlang.org/apidocs/channels/be/dartdoc-view
er/http_parser/http_parser.CompatibleWebSocket |
15 | 15 |
16 ```dart | 16 ```dart |
17 import 'package:shelf/shelf_io.dart' as shelf_io; | 17 import 'package:shelf/shelf_io.dart' as shelf_io; |
18 import 'package:shelf_web_socket/shelf_web_socket.dart'; | 18 import 'package:shelf_web_socket/shelf_web_socket.dart'; |
19 | 19 |
20 void main() { | 20 void main() { |
21 var handler = webSocketHandler((webSocket) { | 21 var handler = webSocketHandler((webSocket) { |
22 webSocket.listen((message) { | 22 webSocket.listen((message) { |
23 webSocket.add("echo $message"); | 23 webSocket.add("echo $message"); |
24 }); | 24 }); |
25 }); | 25 }); |
26 | 26 |
27 shelf_io.serve(handler, 'localhost', 8080).then((server) { | 27 shelf_io.serve(handler, 'localhost', 8080).then((server) { |
28 print('Serving at ws://${server.address.host}:${server.port}'); | 28 print('Serving at ws://${server.address.host}:${server.port}'); |
29 }); | 29 }); |
30 } | 30 } |
31 ``` | 31 ``` |
OLD | NEW |