Close WebSockets

This commit is contained in:
thosakwe 2017-06-06 09:03:24 -04:00
parent 786cfb9d24
commit b09682f515
2 changed files with 23 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:isolate'; import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_websocket/server.dart';
import 'package:glob/glob.dart'; import 'package:glob/glob.dart';
import 'package:html_builder/elements.dart'; import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart'; import 'package:html_builder/html_builder.dart';
@ -177,9 +178,27 @@ class HotReloader {
_handleWatchEvent(WatchEvent e) async { _handleWatchEvent(WatchEvent e) async {
print('${e.path} changed. Reloading server...'); print('${e.path} changed. Reloading server...');
var old = _server; var old = _server;
if (old != null) Future.forEach(old.justBeforeStop, old.configure);
_server = null;
if (old != null) {
// Do this asynchronously, because we really don't care about the old server anymore.
new Future(() async {
// Disconnect active WebSockets
var ws = old.container.make(AngelWebSocket) as AngelWebSocket;
for (var client in ws.clients) {
try {
client.io.close(WebSocketStatus.GOING_AWAY);
} catch (e) {
stderr.writeln(
'Couldn\'t close WebSocket from session #${client.request.session.id}: $e');
}
}
Future.forEach(old.justBeforeStop, old.configure);
});
}
_server = null;
_client ??= _client ??=
new VMServiceClient.connect(vmServiceUrl ?? 'ws://localhost:8181/ws'); new VMServiceClient.connect(vmServiceUrl ?? 'ws://localhost:8181/ws');
var vm = await _client.getVM(); var vm = await _client.getVM();

View file

@ -1,12 +1,13 @@
name: angel_hot name: angel_hot
description: Supports hot reloading of Angel servers on file changes. description: Supports hot reloading of Angel servers on file changes.
version: 1.0.0-rc.2 version: 1.0.0-rc.3
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/hot homepage: https://github.com/angel-dart/hot
environment: environment:
sdk: ">=1.19.0" sdk: ">=1.19.0"
dependencies: dependencies:
angel_framework: ^1.0.0-dev angel_framework: ^1.0.0-dev
angel_websocket: ^1.0.0
html_builder: ^1.0.0 html_builder: ^1.0.0
vm_service_client: vm_service_client:
git: git: