OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:io'; |
| 7 |
| 8 import 'package:shelf/shelf.dart' as shelf; |
| 9 import 'package:shelf_web_socket/shelf_web_socket.dart'; |
| 10 import 'package:scheduled_test/scheduled_server.dart'; |
| 11 |
| 12 /// A class that schedules a server to serve Dart and/or JS code and receive |
| 13 /// WebSocket connections. |
| 14 /// |
| 15 /// This uses [ScheduledServer] under the hood, and has similar semantics: its |
| 16 /// `handle*` methods all schedule a handler that must be hit before the |
| 17 /// schedule will continue. |
| 18 class CodeServer { |
| 19 /// The underlying server. |
| 20 final ScheduledServer _server; |
| 21 |
| 22 /// The URL of the server (including the port), once it's actually |
| 23 /// instantiated. |
| 24 Future<Uri> get url => _server.url; |
| 25 |
| 26 /// The port of the server, once it's actually instantiated. |
| 27 Future<int> get port => _server.port; |
| 28 |
| 29 CodeServer() |
| 30 : _server = new ScheduledServer("code server") { |
| 31 _server.handleUnscheduled("GET", "/favicon.ico", |
| 32 (_) => new shelf.Response.notFound(null)); |
| 33 } |
| 34 |
| 35 /// Sets up a handler for the root of the server, "/", that serves a basic |
| 36 /// HTML page with a script tag that will run [dart]. |
| 37 void handleDart(String dart) { |
| 38 _server.handle("GET", "/", (_) { |
| 39 return new shelf.Response.ok(""" |
| 40 <!doctype html> |
| 41 <html> |
| 42 <head> |
| 43 <script type="application/dart" src="index.dart"></script> |
| 44 </head> |
| 45 </html> |
| 46 """, headers: {'content-type': 'text/html'}); |
| 47 }); |
| 48 |
| 49 _server.handle("GET", "/index.dart", (_) { |
| 50 return new shelf.Response.ok(''' |
| 51 import "dart:html"; |
| 52 |
| 53 main() async { |
| 54 $dart |
| 55 } |
| 56 ''', headers: {'content-type': 'application/dart'}); |
| 57 }); |
| 58 } |
| 59 |
| 60 /// Sets up a handler for the root of the server, "/", that serves a basic |
| 61 /// HTML page with a script tag that will run [javaScript]. |
| 62 void handleJavaScript(String javaScript) { |
| 63 _server.handle("GET", "/", (_) { |
| 64 return new shelf.Response.ok(""" |
| 65 <!doctype html> |
| 66 <html> |
| 67 <head> |
| 68 <script src="index.js"></script> |
| 69 </head> |
| 70 </html> |
| 71 """, headers: {'content-type': 'text/html'}); |
| 72 }); |
| 73 |
| 74 _server.handle("GET", "/index.js", (_) { |
| 75 return new shelf.Response.ok(javaScript, |
| 76 headers: {'content-type': 'application/javascript'}); |
| 77 }); |
| 78 } |
| 79 |
| 80 /// Handles a WebSocket connection to the root of the server, and returns a |
| 81 /// future that will complete to the WebSocket. |
| 82 Future<WebSocket> handleWebSocket() { |
| 83 var completer = new Completer(); |
| 84 _server.handle("GET", "/", webSocketHandler(completer.complete)); |
| 85 return completer.future; |
| 86 } |
| 87 } |
OLD | NEW |