Last active
January 2, 2020 11:26
-
-
Save sethladd/5045819 to your computer and use it in GitHub Desktop.
Registering multiple HTTP handlers for a Dart web server. Not sure if this is the best way or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HttpServer.bind('127.0.0.1', 8889) | |
| .then((HttpServer server) { | |
| var sc = new StreamController(); | |
| sc.stream.transform(new WebSocketTransformer()).listen(handleWebSocket); | |
| server.listen((HttpRequest request) { | |
| if (request.uri.path == '/ws') { | |
| sc.add(request); | |
| } else if (request.uri.path == '/foo') { | |
| request.response.addString('foo'); | |
| request.response.close(); | |
| } else { | |
| print("got 404 for ${request.uri}"); | |
| request.response.statusCode = 404; | |
| request.response.close(); | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:io'; | |
| import 'dart:async'; | |
| handleWebSocket(WebSocket webSocket) { | |
| webSocket.listen((event) { | |
| if (event is MessageEvent) { | |
| /* Handle message. */ | |
| } else if (event is CloseEvent) { | |
| /* Handle closed. */ | |
| } | |
| }); | |
| } | |
| typedef bool ShouldTake(e); | |
| typedef void RouteTo(Stream stream); | |
| typedef void HandleEvent(e); | |
| class TakeAndRoute<S, T> extends StreamEventTransformer<S, T> { | |
| ShouldTake shouldTake; | |
| RouteTo routeTo; | |
| StreamController controller = new StreamController(); | |
| HandleEvent handler; | |
| TakeAndRoute(this.shouldTake, {this.routeTo, this.handler}) { | |
| if (routeTo != null) routeTo(controller.stream); | |
| } | |
| handleData(event, StreamSink sink) { | |
| print("handling"); | |
| if (shouldTake(event)) { | |
| if (routeTo != null) { | |
| controller.add(event); | |
| } | |
| if (handler != null) { | |
| handler(event); | |
| } | |
| } else { | |
| sink.add(event); | |
| } | |
| } | |
| } | |
| main() { | |
| HttpServer.bind('127.0.0.1', 8888) | |
| .then((HttpServer server) { | |
| server | |
| .transform(new TakeAndRoute<HttpRequest, HttpRequest>( | |
| (req) => req.uri.path == '/ws', | |
| routeTo: (stream) => stream.transform(new WebSocketTransformer()).listen(handleWebSocket))) | |
| .transform(new TakeAndRoute<HttpRequest, HttpRequest>( | |
| (req) => req.uri.path == '/foo', | |
| handler: (req) { | |
| print('got foo'); | |
| req.response.addString("foo"); | |
| req.response.close(); | |
| })) | |
| .listen((req) { | |
| print("got 404 for ${req.uri}"); | |
| req.response.statusCode = 404; | |
| req.response.close(); | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment