connect method
Connects to a Kuzzle instance using the provided host name
Implementation
Future<void> connect() {
if (protocol.isReady()) {
return Future.value();
}
if (protocol.state == KuzzleProtocolState.connecting ||
protocol.state == KuzzleProtocolState.reconnecting) {
final completer = Completer<void>();
protocol.once(ProtocolEvents.RECONNECT, completer.complete);
protocol.once(ProtocolEvents.CONNECT, completer.complete);
return completer.future;
}
if (autoQueue) {
startQueuing();
}
protocol.on(ProtocolEvents.CONNECT, () {
if (autoQueue) {
stopQueuing();
}
if (autoReplay) {
playQueue();
}
emit(KuzzleEvents.CONNECTED);
});
protocol.on(ProtocolEvents.NETWORK_ERROR, (error) {
if (autoQueue) {
startQueuing();
}
emit(ProtocolEvents.NETWORK_ERROR, [error]);
});
protocol.on(ProtocolEvents.DISCONNECT, () {
_requests.clear();
emit(KuzzleEvents.DISCONNECTED);
});
protocol.on(ProtocolEvents.RECONNECT, () {
if (autoQueue) {
stopQueuing();
}
if (autoReplay) {
playQueue();
}
if (jwt == null) {
emit(KuzzleEvents.RECONNECTED);
return;
}
auth.checkToken(jwt).then((result) {
// shouldn't obtain an error but let's invalidate the token anyway
if (result['valid'] is! bool && result['valid'] == false) {
jwt = null;
}
emit(KuzzleEvents.RECONNECTED);
}).catchError((_) {
jwt = null;
emit(KuzzleEvents.RECONNECTED);
});
});
return protocol.connect();
}