forked from prijindal/kuzzle_dart
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from kuzzleio/2.3.1-proposal
Release 2.4.1
- Loading branch information
Showing
8 changed files
with
234 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:http/browser_client.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
BaseClient createHttpClient({bool acceptBadCertificate = false}) { | ||
return BrowserClient(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:http/http.dart'; | ||
import 'package:http/io_client.dart'; | ||
|
||
BaseClient createHttpClient({bool acceptBadCertificate = false}) { | ||
final client = HttpClient() | ||
..badCertificateCallback = ((cert, host, port) => acceptBadCertificate); | ||
return IOClient(client); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,16 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:kuzzle/src/kuzzle/response.dart'; | ||
import 'package:pedantic/pedantic.dart'; | ||
import 'package:kuzzle/src/protocols/websocket_io.dart' | ||
if (dart.library.html) 'package:kuzzle/src/protocols/websocket_browser.dart'; | ||
|
||
import '../kuzzle/errors.dart'; | ||
import '../kuzzle/request.dart'; | ||
|
||
import 'abstract.dart'; | ||
import 'events.dart'; | ||
|
||
class WebSocketProtocol extends KuzzleProtocol { | ||
class WebSocketProtocol extends KuzzleWebSocket { | ||
WebSocketProtocol( | ||
Uri uri, { | ||
bool autoReconnect = true, | ||
Duration reconnectionDelay, | ||
Duration pingInterval, | ||
}) : _pingInterval = pingInterval, | ||
super( | ||
}) : super( | ||
uri, | ||
autoReconnect: autoReconnect, | ||
reconnectionDelay: reconnectionDelay | ||
reconnectionDelay: reconnectionDelay, | ||
pingInterval: pingInterval, | ||
); | ||
|
||
String _lastUrl; | ||
WebSocket _webSocket; | ||
StreamSubscription _subscription; | ||
Duration _pingInterval; | ||
Duration get pingInterval => _pingInterval; | ||
set pingInterval(Duration value) { | ||
_pingInterval = value; | ||
_webSocket?.pingInterval = value; | ||
} | ||
|
||
@override | ||
Future<void> connect() async { | ||
final url = '${uri.scheme}://${uri.host}:${uri.port}'; | ||
|
||
await super.connect(); | ||
|
||
if (url != _lastUrl) { | ||
wasConnected = false; | ||
_lastUrl = url; | ||
} | ||
|
||
await _subscription?.cancel(); | ||
_subscription = null; | ||
|
||
await _webSocket?.close(); | ||
_webSocket = null; | ||
|
||
try { | ||
_webSocket = await WebSocket.connect(url); | ||
} on IOException { | ||
if (wasConnected) { | ||
clientNetworkError( | ||
KuzzleError('WebSocketProtocol: Unable to connect to $url')); | ||
|
||
return; | ||
} | ||
|
||
rethrow; | ||
} | ||
|
||
_webSocket.pingInterval = _pingInterval; | ||
|
||
_subscription = _webSocket.listen(_handlePayload, | ||
onError: _handleError, onDone: _handleDone); | ||
|
||
clientConnected(); | ||
|
||
unawaited(_webSocket.done.then((error) { | ||
// print('WebSocketProtocol done'); | ||
// print(error.runtimeType); | ||
clientNetworkError( | ||
KuzzleError('WebSocketProtocol: connection with $url closed')); | ||
})); | ||
} | ||
|
||
@override | ||
Future<KuzzleResponse> send(KuzzleRequest request) { | ||
if (_webSocket != null && _webSocket.readyState == WebSocket.open) { | ||
_webSocket.add(json.encode(request)); | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
void close() { | ||
super.close(); | ||
|
||
removeAllListeners(); | ||
stopRetryingToConnect = true; | ||
wasConnected = false; | ||
|
||
_subscription?.cancel(); | ||
_subscription = null; | ||
|
||
_webSocket?.close(); | ||
_webSocket = null; | ||
} | ||
|
||
void _handlePayload(dynamic payload) { | ||
emit(ProtocolEvents.NETWORK_ON_RESPONSE_RECEIVED, [payload]); | ||
} | ||
|
||
void _handleError(dynamic error, StackTrace stackTrace) { | ||
if (error is Error) { | ||
clientNetworkError(error); | ||
} else { | ||
clientNetworkError(KuzzleError('websocket.onError')); | ||
} | ||
} | ||
|
||
void _handleDone() { | ||
if (_webSocket.closeCode == 1000) { | ||
clientDisconnected(); | ||
} else if (wasConnected) { | ||
clientNetworkError( | ||
KuzzleError('clientNetworkError', | ||
_webSocket.closeReason, | ||
_webSocket.closeCode) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:html'; | ||
import 'package:kuzzle/src/kuzzle/response.dart'; | ||
|
||
import '../kuzzle/errors.dart'; | ||
import '../kuzzle/request.dart'; | ||
|
||
import 'abstract.dart'; | ||
import 'events.dart'; | ||
|
||
class KuzzleWebSocket extends KuzzleProtocol { | ||
KuzzleWebSocket( | ||
Uri uri, { | ||
bool autoReconnect = true, | ||
Duration reconnectionDelay, | ||
this.pingInterval, | ||
}) : super(uri, | ||
autoReconnect: autoReconnect, reconnectionDelay: reconnectionDelay); | ||
|
||
String _lastUrl; | ||
WebSocket _webSocket; | ||
StreamSubscription _subscription; | ||
Duration pingInterval; | ||
Completer<void> _connected = Completer(); | ||
|
||
@override | ||
Future<void> connect() async { | ||
final url = '${uri.scheme}://${uri.host}:${uri.port}'; | ||
|
||
_webSocket ??= WebSocket(url); | ||
|
||
await super.connect(); | ||
|
||
if (url != _lastUrl) { | ||
wasConnected = false; | ||
_lastUrl = url; | ||
} | ||
|
||
await _subscription?.cancel(); | ||
_subscription = null; | ||
|
||
_subscription = _webSocket.onMessage.listen(_handlePayload); | ||
_webSocket.onError.listen(_handleError); | ||
_webSocket.onClose.listen(_handleDone); | ||
|
||
_webSocket.onOpen.listen((_) { | ||
clientConnected(); | ||
_connected.complete(); | ||
}); | ||
return _connected.future; | ||
} | ||
|
||
@override | ||
Future<KuzzleResponse> send(KuzzleRequest request) { | ||
if (_webSocket != null && _webSocket.readyState == WebSocket.OPEN) { | ||
_webSocket.sendString(json.encode(request)); | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
void close() { | ||
super.close(); | ||
|
||
removeAllListeners(); | ||
stopRetryingToConnect = true; | ||
wasConnected = false; | ||
|
||
_subscription?.cancel(); | ||
_subscription = null; | ||
|
||
_webSocket?.close(); | ||
_webSocket = null; | ||
} | ||
|
||
void _handlePayload(MessageEvent payload) { | ||
emit(ProtocolEvents.NETWORK_ON_RESPONSE_RECEIVED, [payload.data]); | ||
} | ||
|
||
void _handleError(Event event) { | ||
clientNetworkError(event); | ||
} | ||
|
||
void _handleDone(Event event) { | ||
clientDisconnected(); | ||
} | ||
} |
Oops, something went wrong.