Skip to content

Commit

Permalink
Merge pull request #80 from kuzzleio/2.3.1-proposal
Browse files Browse the repository at this point in the history
Release 2.4.1
  • Loading branch information
jenow authored Apr 1, 2021
2 parents db156dd + 96c44a0 commit f0122a0
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 127 deletions.
3 changes: 3 additions & 0 deletions .ci/doc/project/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Tests
version: 1.0.0
description: Dart SDK doc snippet test project

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
kuzzle:
path: /mnt/
Expand Down
15 changes: 6 additions & 9 deletions lib/src/protocols/http.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import 'dart:convert';
import 'dart:io';

import 'package:http/io_client.dart';
import 'package:http/http.dart';
import 'package:kuzzle/src/kuzzle/request.dart';
import 'package:kuzzle/src/kuzzle/response.dart';
import 'package:kuzzle/src/protocols/abstract.dart';
import 'package:kuzzle/src/protocols/http_client_io.dart'
if (dart.library.html) 'package:kuzzle/src/protocols/http_client_browser.dart';

class HttpProtocol extends KuzzleProtocol {
HttpProtocol(Uri uri, {bool acceptBadCertificate = false}) : super(uri) {
_client = HttpClient()
..badCertificateCallback =
((cert, host, port) => acceptBadCertificate);
_ioClient = IOClient(_client);
_ioClient = createHttpClient(acceptBadCertificate: acceptBadCertificate);
}

HttpClient _client;
IOClient _ioClient;
BaseClient _ioClient;

@override
Future<void> connect() async {
Expand Down Expand Up @@ -50,7 +47,7 @@ class HttpProtocol extends KuzzleProtocol {
return Future.error(res);
}
return Future.value(
KuzzleResponse.fromJson(jsonDecode(res.body) as Map<String, dynamic>),
KuzzleResponse.fromJson(jsonDecode(utf8.decode(res.bodyBytes)) as Map<String, dynamic>),
);
}
}
6 changes: 6 additions & 0 deletions lib/src/protocols/http_client_browser.dart
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();
}
10 changes: 10 additions & 0 deletions lib/src/protocols/http_client_io.dart
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);
}
123 changes: 6 additions & 117 deletions lib/src/protocols/websocket.dart
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)
);
}
}
}
88 changes: 88 additions & 0 deletions lib/src/protocols/websocket_browser.dart
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();
}
}
Loading

0 comments on commit f0122a0

Please sign in to comment.