From b09682f5152bdda2ddc363f3ce571e021cae0f2f Mon Sep 17 00:00:00 2001 From: thosakwe Date: Tue, 6 Jun 2017 09:03:24 -0400 Subject: [PATCH] Close WebSockets --- lib/angel_hot.dart | 23 +++++++++++++++++++++-- pubspec.yaml | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/angel_hot.dart b/lib/angel_hot.dart index 4542bb5f..e14192a5 100644 --- a/lib/angel_hot.dart +++ b/lib/angel_hot.dart @@ -4,6 +4,7 @@ import 'dart:convert'; import 'dart:io'; import 'dart:isolate'; import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_websocket/server.dart'; import 'package:glob/glob.dart'; import 'package:html_builder/elements.dart'; import 'package:html_builder/html_builder.dart'; @@ -177,9 +178,27 @@ class HotReloader { _handleWatchEvent(WatchEvent e) async { print('${e.path} changed. Reloading 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 ??= new VMServiceClient.connect(vmServiceUrl ?? 'ws://localhost:8181/ws'); var vm = await _client.getVM(); diff --git a/pubspec.yaml b/pubspec.yaml index b90b2fe3..258157ba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,12 +1,13 @@ name: angel_hot 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 homepage: https://github.com/angel-dart/hot environment: sdk: ">=1.19.0" dependencies: angel_framework: ^1.0.0-dev + angel_websocket: ^1.0.0 html_builder: ^1.0.0 vm_service_client: git: