From 7186fa799065430be7c2003a83d05dd8fc1905d2 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sat, 10 Apr 2021 23:12:43 +0800 Subject: [PATCH] Migrated angel_websocket --- CHANGELOG.md | 2 +- .../lib/src/core/hooked_service.dart | 4 +- packages/framework/lib/src/core/routable.dart | 3 +- packages/framework/lib/src/core/server.dart | 4 +- .../lib/src/safe_stream_controller.dart | 2 +- packages/websocket/example/main.dart | 14 ++-- packages/websocket/lib/angel_websocket.dart | 5 +- .../websocket/lib/base_websocket_client.dart | 33 +++++--- packages/websocket/lib/browser.dart | 13 +-- packages/websocket/lib/flutter.dart | 1 - packages/websocket/lib/hooks.dart | 7 +- packages/websocket/lib/io.dart | 3 +- packages/websocket/lib/server.dart | 79 ++++++++++--------- packages/websocket/lib/websocket_context.dart | 15 ++-- .../websocket/lib/websocket_controller.dart | 26 +++--- packages/websocket/test/auth_test.dart | 1 - .../websocket/test/controller/common.dart | 2 +- packages/websocket/test/service/common.dart | 4 +- packages/websocket/web/main.dart | 4 +- 19 files changed, 122 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3149ebf..474c9c2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ * Updated angel_validate to 4.0.0 (6/7 test passed) * Updated json_god to 4.0.0 (13/13 test passed) * Updated angel_client to 4.0.0 (6/13 test passed) -* Updated angel_websocket to 3.0.0 (in progress) +* Updated angel_websocket to 4.0.0 (in progress) * Updated test to 3.0.0 (in progress) * Updated jael to 3.0.0 (in progress) * Updated jael_preprocessor to 3.0.0 (in progress) diff --git a/packages/framework/lib/src/core/hooked_service.dart b/packages/framework/lib/src/core/hooked_service.dart index 2bd0e237..2149f14c 100644 --- a/packages/framework/lib/src/core/hooked_service.dart +++ b/packages/framework/lib/src/core/hooked_service.dart @@ -544,8 +544,8 @@ class HookedServiceEvent> { } /// Triggered on a hooked service event. -typedef FutureOr HookedServiceEventListener>(HookedServiceEvent event); +typedef HookedServiceEventListener> + = FutureOr Function(HookedServiceEvent event); /// Can be listened to, but events may be canceled. class HookedServiceEventDispatcher> { diff --git a/packages/framework/lib/src/core/routable.dart b/packages/framework/lib/src/core/routable.dart index 09ccfa5a..17ee7e57 100644 --- a/packages/framework/lib/src/core/routable.dart +++ b/packages/framework/lib/src/core/routable.dart @@ -15,7 +15,8 @@ import 'service.dart'; final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)'); /// A function that receives an incoming [RequestContext] and responds to it. -typedef FutureOr RequestHandler(RequestContext req, ResponseContext res); +typedef RequestHandler = FutureOr Function( + RequestContext req, ResponseContext res); /// Sequentially runs a list of [handlers] of middleware, and returns early if any does not /// return `true`. Works well with [Router].chain. diff --git a/packages/framework/lib/src/core/server.dart b/packages/framework/lib/src/core/server.dart index 96b83916..24e4af51 100644 --- a/packages/framework/lib/src/core/server.dart +++ b/packages/framework/lib/src/core/server.dart @@ -22,10 +22,10 @@ import 'service.dart'; //final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)'); /// A function that configures an [Angel] server in some way. -typedef FutureOr AngelConfigurer(Angel app); +typedef AngelConfigurer = FutureOr Function(Angel app); /// A function that asynchronously generates a view from the given path and data. -typedef FutureOr ViewGenerator(String path, +typedef ViewGenerator = FutureOr Function(String path, [Map? data]); /// A powerful real-time/REST/MVC server class. diff --git a/packages/framework/lib/src/safe_stream_controller.dart b/packages/framework/lib/src/safe_stream_controller.dart index 70837bde..291b51a4 100644 --- a/packages/framework/lib/src/safe_stream_controller.dart +++ b/packages/framework/lib/src/safe_stream_controller.dart @@ -1,6 +1,6 @@ import 'dart:async'; -typedef void _InitCallback(); +typedef _InitCallback = void Function(); /// A [StreamController] boilerplate that prevents memory leaks. abstract class SafeCtrl { diff --git a/packages/websocket/example/main.dart b/packages/websocket/example/main.dart index cf3746dc..70242996 100644 --- a/packages/websocket/example/main.dart +++ b/packages/websocket/example/main.dart @@ -6,12 +6,12 @@ import 'package:angel_websocket/server.dart'; import 'package:file/local.dart'; import 'package:logging/logging.dart'; -main(List args) async { - var app = new Angel(); - var http = new AngelHttp(app); - var ws = new AngelWebSocket(app, sendErrors: !app.environment.isProduction); +void main(List args) async { + var app = Angel(); + var http = AngelHttp(app); + var ws = AngelWebSocket(app, sendErrors: !app.environment.isProduction); var fs = const LocalFileSystem(); - app.logger = new Logger('angel_websocket'); + app.logger = Logger('angel_websocket'); // This is a plug-in. It hooks all your services, // to automatically broadcast events. @@ -31,7 +31,7 @@ main(List args) async { }); if (args.contains('http2')) { - var ctx = new SecurityContext() + var ctx = SecurityContext() ..useCertificateChain('dev.pem') ..usePrivateKey('dev.key', password: 'dartdart'); @@ -45,7 +45,7 @@ main(List args) async { ); } - var http2 = new AngelHttp2(app, ctx); + var http2 = AngelHttp2(app, ctx); http2.onHttp1.listen(http.handleRequest); await http2.startServer('127.0.0.1', 3000); print('Listening at ${http2.uri}'); diff --git a/packages/websocket/lib/angel_websocket.dart b/packages/websocket/lib/angel_websocket.dart index cf166100..c602aa79 100644 --- a/packages/websocket/lib/angel_websocket.dart +++ b/packages/websocket/lib/angel_websocket.dart @@ -6,7 +6,7 @@ class WebSocketEvent { String eventName; Data data; - WebSocketEvent({String this.eventName, this.data}); + WebSocketEvent({this.eventName, this.data}); factory WebSocketEvent.fromJson(Map data) => WebSocketEvent( eventName: data['eventName'].toString(), data: data['data'] as Data); @@ -31,8 +31,7 @@ class WebSocketAction { var data; Map params; - WebSocketAction( - {String this.id, String this.eventName, this.data, this.params}); + WebSocketAction({this.id, this.eventName, this.data, this.params}); factory WebSocketAction.fromJson(Map data) => WebSocketAction( id: data['id'].toString(), diff --git a/packages/websocket/lib/base_websocket_client.dart b/packages/websocket/lib/base_websocket_client.dart index c4588907..91cc8ebf 100644 --- a/packages/websocket/lib/base_websocket_client.dart +++ b/packages/websocket/lib/base_websocket_client.dart @@ -10,7 +10,7 @@ import 'package:web_socket_channel/status.dart' as status; import 'angel_websocket.dart'; import 'constants.dart'; -final RegExp _straySlashes = RegExp(r"(^/)|(/+$)"); +final RegExp _straySlashes = RegExp(r'(^/)|(/+$)'); /// An [Angel] client that operates across WebSockets. abstract class BaseWebSocketClient extends BaseAngelClient { @@ -38,6 +38,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient { Stream get onAllEvents => _onAllEvents.stream; /// Fired whenever a WebSocket is successfully authenticated. + @override Stream get onAuthenticated => _onAuthenticated.stream; /// A broadcast stream of data coming from the [socket]. @@ -143,6 +144,8 @@ abstract class BaseWebSocketClient extends BaseAngelClient { return await c.future.then((socket) { _socket = socket; listen(); + + return _socket; }); } else { _socket = await getConnectedWebSocket(); @@ -159,7 +162,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient { @override WebSocketsService service(String path, {Type type, AngelDeserializer deserializer}) { - String uri = path.toString().replaceAll(_straySlashes, ''); + var uri = path.toString().replaceAll(_straySlashes, ''); return WebSocketsService(socket, this, uri, deserializer: deserializer); } @@ -192,7 +195,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient { _onAuthenticated.add(authResult); } else if (event.eventName?.isNotEmpty == true) { var split = event.eventName - .split("::") + .split('::') .where((str) => str.isNotEmpty) .toList(); @@ -225,14 +228,15 @@ abstract class BaseWebSocketClient extends BaseAngelClient { } /// Serializes data to JSON. - serialize(x) => json.encode(x); + dynamic serialize(x) => json.encode(x); /// Sends the given [action] on the [socket]. void sendAction(WebSocketAction action) { - if (_socket == null) + if (_socket == null) { _queue.addLast(action); - else + } else { socket.sink.add(serialize(action)); + } } /// Attempts to authenticate a WebSocket, using a valid JWT. @@ -274,27 +278,34 @@ class WebSocketsService extends Service { Stream get onAllEvents => _onAllEvents.stream; /// Fired on `index` events. + @override Stream> get onIndexed => _onIndexed.stream; /// Fired on `read` events. + @override Stream get onRead => _onRead.stream; /// Fired on `created` events. + @override Stream get onCreated => _onCreated.stream; /// Fired on `modified` events. + @override Stream get onModified => _onModified.stream; /// Fired on `updated` events. + @override Stream get onUpdated => _onUpdated.stream; /// Fired on `removed` events. + @override Stream get onRemoved => _onRemoved.stream; WebSocketsService(this.socket, this.app, this.path, {this.deserializer}) { listen(); } + @override Future close() async { await _onAllEvents.close(); await _onCreated.close(); @@ -306,7 +317,7 @@ class WebSocketsService extends Service { } /// Serializes an [action] to be sent over a WebSocket. - serialize(WebSocketAction action) => json.encode(action); + dynamic serialize(WebSocketAction action) => json.encode(action); /// Deserializes data from a [WebSocketEvent]. Data deserialize(x) { @@ -423,18 +434,20 @@ class WebSocketsService extends Service { /// Contains a dynamic Map of [WebSocketEvent] streams. class WebSocketExtraneousEventHandler { - Map> _events = {}; + final Map> _events = {}; StreamController _getStream(String index) { - if (_events[index] == null) + if (_events[index] == null) { _events[index] = StreamController(); + } return _events[index]; } Stream operator [](String index) { - if (_events[index] == null) + if (_events[index] == null) { _events[index] = StreamController(); + } return _events[index].stream; } diff --git a/packages/websocket/lib/browser.dart b/packages/websocket/lib/browser.dart index 829e4970..9fab0898 100644 --- a/packages/websocket/lib/browser.dart +++ b/packages/websocket/lib/browser.dart @@ -11,7 +11,7 @@ import 'package:web_socket_channel/html.dart'; import 'base_websocket_client.dart'; export 'angel_websocket.dart'; -final RegExp _straySlashes = RegExp(r"(^/)|(/+$)"); +final RegExp _straySlashes = RegExp(r'(^/)|(/+$)'); /// Queries an Angel server via WebSockets. class WebSockets extends BaseWebSocketClient { @@ -50,8 +50,9 @@ class WebSockets extends BaseWebSocketClient { timer.cancel(); sub?.cancel(); } - } else + } else { timer.cancel(); + } }); sub = window.on[eventName ?? 'token'].listen((e) { @@ -81,12 +82,14 @@ class WebSockets extends BaseWebSocketClient { socket ..onOpen.listen((_) { - if (!completer.isCompleted) + if (!completer.isCompleted) { return completer.complete(HtmlWebSocketChannel(socket)); + } }) ..onError.listen((e) { - if (!completer.isCompleted) + if (!completer.isCompleted) { return completer.completeError(e is ErrorEvent ? e.error : e); + } }); return completer.future; @@ -95,7 +98,7 @@ class WebSockets extends BaseWebSocketClient { @override BrowserWebSocketsService service(String path, {Type type, AngelDeserializer deserializer}) { - String uri = path.replaceAll(_straySlashes, ''); + var uri = path.replaceAll(_straySlashes, ''); return BrowserWebSocketsService(socket, this, uri, deserializer: deserializer); } diff --git a/packages/websocket/lib/flutter.dart b/packages/websocket/lib/flutter.dart index 3005e403..0d47b9fe 100644 --- a/packages/websocket/lib/flutter.dart +++ b/packages/websocket/lib/flutter.dart @@ -3,7 +3,6 @@ library angel_websocket.flutter; import 'dart:async'; import 'dart:io'; -import 'package:http/http.dart' as http; import 'package:http/io_client.dart' as http; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/io.dart'; diff --git a/packages/websocket/lib/hooks.dart b/packages/websocket/lib/hooks.dart index 362e5d62..92450e31 100644 --- a/packages/websocket/lib/hooks.dart +++ b/packages/websocket/lib/hooks.dart @@ -7,8 +7,8 @@ import 'package:angel_framework/angel_framework.dart'; HookedServiceEventListener doNotBroadcast([provider]) { return (HookedServiceEvent e) { if (e.params != null && e.params.containsKey('provider')) { - bool deny = false; - Iterable providers = provider is Iterable ? provider : [provider]; + var deny = false; + var providers = provider is Iterable ? provider : [provider]; for (var p in providers) { if (deny) break; @@ -19,9 +19,10 @@ HookedServiceEventListener doNotBroadcast([provider]) { e.params['provider'] == p.via; } else if (p == null) { deny = true; - } else + } else { deny = deny || (e.params['provider'] as Providers).via == p.toString(); + } } e.params['broadcast'] = false; diff --git a/packages/websocket/lib/io.dart b/packages/websocket/lib/io.dart index 7c6980f7..229d7517 100644 --- a/packages/websocket/lib/io.dart +++ b/packages/websocket/lib/io.dart @@ -4,7 +4,6 @@ library angel_websocket.io; import 'dart:async'; import 'dart:io'; import 'package:angel_client/angel_client.dart'; -import 'package:http/http.dart' as http; import 'package:http/io_client.dart' as http; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/io.dart'; @@ -12,7 +11,7 @@ import 'base_websocket_client.dart'; export 'package:angel_client/angel_client.dart'; export 'angel_websocket.dart'; -final RegExp _straySlashes = RegExp(r"(^/)|(/+$)"); +final RegExp _straySlashes = RegExp(r'(^/)|(/+$)'); /// Queries an Angel server via WebSockets. class WebSockets extends BaseWebSocketClient { diff --git a/packages/websocket/lib/server.dart b/packages/websocket/lib/server.dart index d12ab449..7107951c 100644 --- a/packages/websocket/lib/server.dart +++ b/packages/websocket/lib/server.dart @@ -21,11 +21,11 @@ part 'websocket_context.dart'; part 'websocket_controller.dart'; -typedef String WebSocketResponseSerializer(data); +typedef WebSocketResponseSerializer = String Function(dynamic data); /// Broadcasts events from [HookedService]s, and handles incoming [WebSocketAction]s. class AngelWebSocket { - List _clients = []; + final List _clients = []; final List _servicesAlreadyWired = []; final StreamController _onAction = @@ -118,7 +118,7 @@ class AngelWebSocket { /// Slates an event to be dispatched. Future batchEvent(WebSocketEvent event, - {filter(WebSocketContext socket), bool notify = true}) async { + {Function(WebSocketContext socket) filter, bool notify = true}) async { // Default implementation will just immediately fire events _clients.forEach((client) async { dynamic result = true; @@ -128,8 +128,9 @@ class AngelWebSocket { } }); - if (synchronizationChannel != null && notify != false) + if (synchronizationChannel != null && notify != false) { synchronizationChannel.sink.add(event); + } } /// Returns a list of events yet to be sent. @@ -137,7 +138,7 @@ class AngelWebSocket { /// Responds to an incoming action on a WebSocket. Future handleAction(WebSocketAction action, WebSocketContext socket) async { - var split = action.eventName.split("::"); + var split = action.eventName.split('::'); if (split.length < 2) { socket.sendError(AngelHttpException.badRequest()); @@ -148,7 +149,7 @@ class AngelWebSocket { if (service == null) { socket.sendError(AngelHttpException.notFound( - message: "No service \"${split[0]}\" exists.")); + message: 'No service \"${split[0]}\" exists.')); return null; } @@ -157,17 +158,18 @@ class AngelWebSocket { if (action.params is! Map) action.params = {}; if (allowClientParams != true) { - if (action.params['query'] is Map) + if (action.params['query'] is Map) { action.params = {'query': action.params['query']}; - else + } else { action.params = {}; + } } var params = mergeMap([ ((deserializer ?? (params) => params)(action.params)) as Map, { - "provider": Providers.websocket, + 'provider': Providers.websocket, '__requestctx': socket.request, '__responsectx': socket.response } @@ -176,31 +178,31 @@ class AngelWebSocket { try { if (actionName == indexAction) { socket.send( - "${split[0]}::" + indexedEvent, await service.index(params)); + '${split[0]}::' + indexedEvent, await service.index(params)); return null; } else if (actionName == readAction) { socket.send( - "${split[0]}::" + readEvent, await service.read(action.id, params)); + '${split[0]}::' + readEvent, await service.read(action.id, params)); return null; } else if (actionName == createAction) { return WebSocketEvent( - eventName: "${split[0]}::" + createdEvent, + eventName: '${split[0]}::' + createdEvent, data: await service.create(action.data, params)); } else if (actionName == modifyAction) { return WebSocketEvent( - eventName: "${split[0]}::" + modifiedEvent, + eventName: '${split[0]}::' + modifiedEvent, data: await service.modify(action.id, action.data, params)); } else if (actionName == updateAction) { return WebSocketEvent( - eventName: "${split[0]}::" + updatedEvent, + eventName: '${split[0]}::' + updatedEvent, data: await service.update(action.id, action.data, params)); } else if (actionName == removeAction) { return WebSocketEvent( - eventName: "${split[0]}::" + removedEvent, + eventName: '${split[0]}::' + removedEvent, data: await service.remove(action.id, params)); } else { socket.sendError(AngelHttpException.methodNotAllowed( - message: "Method Not Allowed: \"$actionName\"")); + message: 'Method Not Allowed: \"$actionName\"')); return null; } } catch (e, st) { @@ -223,7 +225,7 @@ class AngelWebSocket { var user = await auth.deserializer(token.userId); socket.request ..container.registerSingleton(token) - ..container.registerSingleton(user, as: user.runtimeType as Type); + ..container.registerSingleton(user, as: user.runtimeType); socket._onAuthenticated.add(null); socket.send(authenticatedEvent, {'token': token.serialize(auth.hmac), 'data': user}); @@ -257,7 +259,7 @@ class AngelWebSocket { Future handleConnect(WebSocketContext socket) async {} /// Handles incoming data from a WebSocket. - handleData(WebSocketContext socket, data) async { + dynamic handleData(WebSocketContext socket, data) async { try { socket._onData.add(data); var fromJson = json.decode(data.toString()); @@ -270,18 +272,19 @@ class AngelWebSocket { throw AngelHttpException.badRequest(); } - if (fromJson is Map && fromJson.containsKey("eventName")) { + if (fromJson is Map && fromJson.containsKey('eventName')) { socket._onAction.add(WebSocketAction.fromJson(fromJson)); socket.on - ._getStreamForEvent(fromJson["eventName"].toString()) - .add(fromJson["data"] as Map); + ._getStreamForEvent(fromJson['eventName'].toString()) + .add(fromJson['data'] as Map); } - if (action.eventName == authenticateAction) + if (action.eventName == authenticateAction) { await handleAuth(action, socket); + } - if (action.eventName.contains("::")) { - var split = action.eventName.split("::"); + if (action.eventName.contains('::')) { + var split = action.eventName.split('::'); if (split.length >= 2) { if (actions.contains(split[1])) { @@ -318,8 +321,8 @@ class AngelWebSocket { } /// Hooks any [HookedService]s that are not being broadcasted yet. - wireAllServices(Angel app) { - for (Pattern key in app.services.keys.where((x) { + void wireAllServices(Angel app) { + for (var key in app.services.keys.where((x) { return !_servicesAlreadyWired.contains(x) && app.services[x] is HookedService; })) { @@ -329,10 +332,11 @@ class AngelWebSocket { /// Configures an [Angel] instance to listen for WebSocket connections. Future configureServer(Angel app) async { - app..container.registerSingleton(this); + app.container.registerSingleton(this); - if (runtimeType != AngelWebSocket) - app..container.registerSingleton(this); + if (runtimeType != AngelWebSocket) { + app.container.registerSingleton(this); + } // Set up services wireAllServices(app); @@ -384,9 +388,10 @@ class AngelWebSocket { /// Handles an incoming HTTP request. Future handleRequest(RequestContext req, ResponseContext res) async { if (req is HttpRequestContext && res is HttpResponseContext) { - if (!WebSocketTransformer.isUpgradeRequest(req.rawRequest)) + if (!WebSocketTransformer.isUpgradeRequest(req.rawRequest)) { throw AngelHttpException.badRequest(); - await res.detach(); + } + res.detach(); var ws = await WebSocketTransformer.upgrade(req.rawRequest); var channel = IOWebSocketChannel(ws); var socket = WebSocketContext(channel, req, res); @@ -437,12 +442,12 @@ class AngelWebSocket { } var sink = utf8.encoder.startChunkedConversion(ctrl.foreign.sink); - sink.add("HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n"); - if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n"); - sink.add("\r\n"); + sink.add('HTTP/1.1 101 Switching Protocols\r\n' + 'Upgrade: websocket\r\n' + 'Connection: Upgrade\r\n' + 'Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n'); + if (protocol != null) sink.add('Sec-WebSocket-Protocol: $protocol\r\n'); + sink.add('\r\n'); var ws = WebSocketChannel(ctrl.foreign); var socket = WebSocketContext(ws, req, res); diff --git a/packages/websocket/lib/websocket_context.dart b/packages/websocket/lib/websocket_context.dart index e175c02d..efe14f54 100644 --- a/packages/websocket/lib/websocket_context.dart +++ b/packages/websocket/lib/websocket_context.dart @@ -15,14 +15,14 @@ class WebSocketContext { /// The original [ResponseContext]. final ResponseContext response; - StreamController _onAction = + final StreamController _onAction = StreamController(); - StreamController _onAuthenticated = StreamController(); + final StreamController _onAuthenticated = StreamController(); - StreamController _onClose = StreamController(); + final StreamController _onClose = StreamController(); - StreamController _onData = StreamController(); + final StreamController _onData = StreamController(); /// Fired on any [WebSocketAction]; Stream get onAction => _onAction.stream; @@ -45,7 +45,7 @@ class WebSocketContext { await _onAction.close(); await _onAuthenticated.close(); await _onData.close(); - await _onClose.add(null); + _onClose.add(null); await _onClose.close(); }); } @@ -61,11 +61,12 @@ class WebSocketContext { } class _WebSocketEventTable { - Map> _handlers = {}; + final Map> _handlers = {}; StreamController _getStreamForEvent(String eventName) { - if (!_handlers.containsKey(eventName)) + if (!_handlers.containsKey(eventName)) { _handlers[eventName] = StreamController(); + } return _handlers[eventName]; } diff --git a/packages/websocket/lib/websocket_controller.dart b/packages/websocket/lib/websocket_controller.dart index 47166118..5ea9d873 100644 --- a/packages/websocket/lib/websocket_controller.dart +++ b/packages/websocket/lib/websocket_controller.dart @@ -12,44 +12,46 @@ class WebSocketController extends Controller { /// The plug-in instance powering this controller. final AngelWebSocket ws; - Map _handlers = {}; - Map _handlerSymbols = {}; + final Map _handlers = {}; + final Map _handlerSymbols = {}; WebSocketController(this.ws) : super(); /// Sends an event to all clients. - void broadcast(String eventName, data, {filter(WebSocketContext socket)}) { + void broadcast(String eventName, data, + {Function(WebSocketContext socket) filter}) { ws.batchEvent(WebSocketEvent(eventName: eventName, data: data), filter: filter); } /// Fired on new connections. - onConnect(WebSocketContext socket) {} + dynamic onConnect(WebSocketContext socket) {} /// Fired on disconnections. - onDisconnect(WebSocketContext socket) {} + dynamic onDisconnect(WebSocketContext socket) {} /// Fired on all incoming actions. - onAction(WebSocketAction action, WebSocketContext socket) async {} + dynamic onAction(WebSocketAction action, WebSocketContext socket) async {} /// Fired on arbitrary incoming data. - onData(data, WebSocketContext socket) {} + dynamic onData(data, WebSocketContext socket) {} @override Future configureServer(Angel app) async { - if (findExpose(app.container.reflector) != null) + if (findExpose(app.container.reflector) != null) { await super.configureServer(app); + } - InstanceMirror instanceMirror = reflect(this); - ClassMirror classMirror = reflectClass(this.runtimeType); + var instanceMirror = reflect(this); + var classMirror = reflectClass(runtimeType); classMirror.instanceMembers.forEach((sym, mirror) { if (mirror.isRegularMethod) { - InstanceMirror exposeMirror = mirror.metadata.firstWhere( + var exposeMirror = mirror.metadata.firstWhere( (mirror) => mirror.reflectee is ExposeWs, orElse: () => null); if (exposeMirror != null) { - ExposeWs exposeWs = exposeMirror.reflectee as ExposeWs; + var exposeWs = exposeMirror.reflectee as ExposeWs; _handlers[exposeWs.eventName] = mirror; _handlerSymbols[exposeWs.eventName] = sym; } diff --git a/packages/websocket/test/auth_test.dart b/packages/websocket/test/auth_test.dart index 10436c01..4972f15e 100644 --- a/packages/websocket/test/auth_test.dart +++ b/packages/websocket/test/auth_test.dart @@ -1,4 +1,3 @@ -import 'dart:async'; import 'package:angel_auth/angel_auth.dart'; import 'package:angel_client/io.dart' as c; import 'package:angel_framework/angel_framework.dart'; diff --git a/packages/websocket/test/controller/common.dart b/packages/websocket/test/controller/common.dart index 2f89ebbc..401fa92e 100644 --- a/packages/websocket/test/controller/common.dart +++ b/packages/websocket/test/controller/common.dart @@ -28,7 +28,7 @@ class GameController extends WebSocketController { GameController(AngelWebSocket ws) : super(ws); @ExposeWs('search') - search(WebSocketContext socket) async { + dynamic search(WebSocketContext socket) async { print('User is searching for a game...'); socket.send('searched', johnVsBob); } diff --git a/packages/websocket/test/service/common.dart b/packages/websocket/test/service/common.dart index 9d327860..a13d7007 100644 --- a/packages/websocket/test/service/common.dart +++ b/packages/websocket/test/service/common.dart @@ -10,7 +10,7 @@ class Todo extends Model { String text; String when; - Todo({String this.text, String this.when}); + Todo({this.text, this.when}); } class TodoService extends MapService { @@ -23,7 +23,7 @@ class TodoService extends MapService { } } -testIndex(BaseWebSocketClient client) async { +dynamic testIndex(BaseWebSocketClient client) async { var todoService = client.service('api/todos'); scheduleMicrotask(() => todoService.index()); diff --git a/packages/websocket/web/main.dart b/packages/websocket/web/main.dart index ffc04ed9..2bb42277 100644 --- a/packages/websocket/web/main.dart +++ b/packages/websocket/web/main.dart @@ -2,8 +2,8 @@ import 'dart:html'; import 'package:angel_websocket/browser.dart'; /// Dummy app to ensure client works with DDC. -main() { - var app = new WebSockets(window.location.origin); +void main() { + var app = WebSockets(window.location.origin); window.alert(app.baseUrl.toString()); app.connect().catchError((_) {