Skip to content

Commit

Permalink
Enable Dart 2.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Stargator committed Nov 23, 2018
1 parent 3931190 commit 7337172
Show file tree
Hide file tree
Showing 9 changed files with 771 additions and 759 deletions.
11 changes: 6 additions & 5 deletions bin/web_server.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "dart:io";
import "dart:async";
import "package:dart2_constant/io.dart" as io;
import "package:web_server/web_server.dart" as webServer;

/**
Expand All @@ -11,7 +12,7 @@ Future<Null> main(final List<String> args) async {
"h": "help"
};
final Map<String, dynamic> cmdLineArgsMap = _parseCmdLineArgs(args, SHORTHAND_TO_FULL_CMD_LINE_ARG_KEYS);
InternetAddress hostAddr = InternetAddress.ANY_IP_V4;
InternetAddress hostAddr = io.InternetAddress.anyIPv4;
int portNumber = 8080; // Default value.

if (cmdLineArgsMap.containsKey('help')) {
Expand Down Expand Up @@ -48,15 +49,15 @@ Future<Null> main(final List<String> args) async {

// Handle errors
localWebServer.httpServerHandler
..onErrorDocument(HttpStatus.NOT_FOUND, (final HttpRequest httpRequest) {
..onErrorDocument(io.HttpStatus.notFound, (final HttpRequest httpRequest) {
// Use the helper method from this WebServer package
webServer.HttpServerRequestHandler.sendPageNotFoundResponse(httpRequest,
'<h1>${HttpStatus.NOT_FOUND} - Page not found</h1>');
'<h1>${io.HttpStatus.notFound} - Page not found</h1>');
})
..onErrorDocument(HttpStatus.INTERNAL_SERVER_ERROR, (final HttpRequest httpRequest) {
..onErrorDocument(io.HttpStatus.internalServerError, (final HttpRequest httpRequest) {
// Use the helper method from this WebServer package
webServer.HttpServerRequestHandler.sendInternalServerErrorResponse(httpRequest,
'<h1>${HttpStatus.INTERNAL_SERVER_ERROR} - Internal Server Error</h1>');
'<h1>${io.HttpStatus.internalServerError} - Internal Server Error</h1>');
});
}

Expand Down
3 changes: 2 additions & 1 deletion example/virtual_directory.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "dart:io";
import "dart:async";
import "package:dart2_constant/io.dart" as io;
import "package:web_server/web_server.dart" as webServer;

Future<Null> main() async {
// Initialize the WebServer
final webServer.WebServer localWebServer = new webServer.WebServer(InternetAddress.ANY_IP_V4, 8080,
final webServer.WebServer localWebServer = new webServer.WebServer(io.InternetAddress.anyIPv4, 8080,
hasHttpServer: true);

// Log out some of the connection information.
Expand Down
11 changes: 6 additions & 5 deletions example/web_server_misc.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import "dart:io";
import "package:dart2_constant/io.dart" as io;
import "package:web_server/web_server.dart";

void main() {
// Initialize and bind the HTTP and WebSocket WebServer
final WebServer localWebServer = new WebServer(InternetAddress.ANY_IP_V4, 8080,
final WebServer localWebServer = new WebServer(io.InternetAddress.anyIPv4, 8080,
hasHttpServer: true, hasWebSocketServer: true);

// Log out some of the connection information
Expand Down Expand Up @@ -41,23 +42,23 @@ void main() {

// Respond to the Url request
httpRequest.response
..headers.contentType = ContentType.JSON // Set the 'content-type' header as JSON
..headers.contentType = io.ContentType.json // Set the 'content-type' header as JSON
..write(apiResponse.toJsonEncoded()) // Export as a JSON encoded string
..close();
})

// Add a custom function for handling the request in case of the error code supplied as the parameter.
..onErrorDocument(HttpStatus.NOT_FOUND, (final HttpRequest httpRequest) {
..onErrorDocument(io.HttpStatus.notFound, (final HttpRequest httpRequest) {
httpRequest.response
..statusCode = HttpStatus.NOT_FOUND
..statusCode = io.HttpStatus.notFound
..headers.contentType = new ContentType('text', 'html', charset: 'utf-8')
..write('<h1>404 Error accessing: ${httpRequest.requestedUri.path}</h1>')
..close();
});

// Attach WebSocket command listeners and base events
localWebServer.webSocketServerHandler
..on[0].listen((final WebSocketRequestPayload requestPayload) { /*...*/ })
..on[0].listen((final dynamic requestPayload) { /*...*/ })
..onConnectionOpen.listen((final WebSocketConnectionData connectionData) { /*...*/ })
..onConnectionError.listen((final WebSocket webSocket) { /*...*/ })
..onConnectionClose.listen((final WebSocket webSocket) { /*...*/ });
Expand Down
4 changes: 2 additions & 2 deletions lib/src/web_server/api_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ApiResponse {
* Calls [toJson], then processes it through [JSON.encode()] before returning.
*/
String toJsonEncoded() {
return JSON.encode(this.toJson());
return convert.json.encode(this.toJson());
}
}

Expand Down Expand Up @@ -112,6 +112,6 @@ class ApiErrorResponse {
}

String toJsonEncoded() {
return JSON.encode(this.toJson());
return convert.json.encode(this.toJson());
}
}
Loading

0 comments on commit 7337172

Please sign in to comment.