From 6e1c1f4191a9db3edecf3ca5305fb0b80b8f20af Mon Sep 17 00:00:00 2001 From: thosakwe Date: Mon, 17 Apr 2017 07:03:42 -0400 Subject: [PATCH] Fixes --- README.md | 2 +- lib/base_websocket_client.dart | 40 ++++++++++++++++++++++------------ pubspec.yaml | 2 +- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 245e8eca..7aec6abb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angel_websocket -[![1.0.4+2](https://img.shields.io/badge/pub-1.0.4+2-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) +[![1.0.4+3](https://img.shields.io/badge/pub-1.0.4+3-brightgreen.svg)](https://pub.dartlang.org/packages/angel_websocket) [![build status](https://travis-ci.org/angel-dart/websocket.svg)](https://travis-ci.org/angel-dart/websocket) WebSocket plugin for Angel. diff --git a/lib/base_websocket_client.dart b/lib/base_websocket_client.dart index e420160d..18081022 100644 --- a/lib/base_websocket_client.dart +++ b/lib/base_websocket_client.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:collection'; import 'dart:convert'; import 'package:angel_client/angel_client.dart'; import 'package:http/src/base_client.dart' as http; @@ -14,6 +15,7 @@ final RegExp _straySlashes = new RegExp(r"(^/)|(/+$)"); abstract class BaseWebSocketClient extends BaseAngelClient { Duration _reconnectInterval; WebSocketChannel _socket; + final Queue _queue = new Queue(); final StreamController _onData = new StreamController(); final StreamController _onAllEvents = @@ -99,6 +101,12 @@ abstract class BaseWebSocketClient extends BaseAngelClient { getConnectedWebSocket().then((socket) { if (!c.isCompleted) { if (timer.isActive) timer.cancel(); + + while (_queue.isNotEmpty) { + var action = _queue.removeFirst(); + socket.sink.add(serialize(action)); + } + c.complete(socket); } }).catchError((e, st) { @@ -174,6 +182,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient { }, cancelOnError: true, onDone: () { + _socket = null; if (reconnectOnClose == true) { new Timer.periodic(reconnectInterval, (Timer timer) async { var result; @@ -199,7 +208,10 @@ abstract class BaseWebSocketClient extends BaseAngelClient { /// Sends the given [action] on the [socket]. void sendAction(WebSocketAction action) { - socket.sink.add(serialize(action)); + if (_socket == null) + _queue.addLast(action); + else + socket.sink.add(serialize(action)); } /// Attempts to authenticate a WebSocket, using a valid JWT. @@ -321,56 +333,56 @@ class BaseWebSocketService extends Service { /// Sends the given [action] on the [socket]. void send(WebSocketAction action) { - socket.sink.add(serialize(action)); + app.sendAction(action); } @override Future index([Map params]) async { - socket.sink.add(serialize(new WebSocketAction( - eventName: '$path::${ACTION_INDEX}', params: params ?? {}))); + app.sendAction(new WebSocketAction( + eventName: '$path::${ACTION_INDEX}', params: params ?? {})); return null; } @override Future read(id, [Map params]) async { - socket.sink.add(serialize(new WebSocketAction( - eventName: '$path::${ACTION_READ}', id: id, params: params ?? {}))); + app.sendAction(new WebSocketAction( + eventName: '$path::${ACTION_READ}', id: id, params: params ?? {})); return null; } @override Future create(data, [Map params]) async { - socket.sink.add(serialize(new WebSocketAction( + app.sendAction(new WebSocketAction( eventName: '$path::${ACTION_CREATE}', data: data, - params: params ?? {}))); + params: params ?? {})); return null; } @override Future modify(id, data, [Map params]) async { - socket.sink.add(serialize(new WebSocketAction( + app.sendAction(new WebSocketAction( eventName: '$path::${ACTION_MODIFY}', id: id, data: data, - params: params ?? {}))); + params: params ?? {})); return null; } @override Future update(id, data, [Map params]) async { - socket.sink.add(serialize(new WebSocketAction( + app.sendAction(new WebSocketAction( eventName: '$path::${ACTION_UPDATE}', id: id, data: data, - params: params ?? {}))); + params: params ?? {})); return null; } @override Future remove(id, [Map params]) async { - socket.sink.add(serialize(new WebSocketAction( - eventName: '$path::${ACTION_REMOVE}', id: id, params: params ?? {}))); + app.sendAction(new WebSocketAction( + eventName: '$path::${ACTION_REMOVE}', id: id, params: params ?? {})); return null; } } diff --git a/pubspec.yaml b/pubspec.yaml index a55dca17..e93b8104 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: angel_websocket description: WebSocket plugin for Angel. environment: sdk: ">=1.19.0" -version: 1.0.4+2 +version: 1.0.4+3 author: Tobe O homepage: https://github.com/angel-dart/angel_websocket dependencies: