connect method
Implementation
Future<void> connect() async {
if (_state == WebSocketConnectionState.connected ||
_state == WebSocketConnectionState.connecting) {
return;
}
_manuallyClosed = false;
_setState(WebSocketConnectionState.connecting);
try {
final wsUrl = await _buildUrl();
if (debug) print('Connecting WebSocket to $wsUrl');
final socket = web.WebSocket(wsUrl);
_socket = socket;
socket.onOpen.listen((_) {
_setState(WebSocketConnectionState.connected);
_reconnectAttempts = 0;
_lastPong = DateTime.now();
_emitLocal('connect');
_emitLocal('open');
_startHeartbeat();
_flushMessageQueue();
});
socket.onMessage.listen((event) {
_handleMessage(event.data.dartify());
});
socket.onError.listen((event) {
_emitLocal('error', event);
_handleDisconnect(event);
});
socket.onClose.listen((event) {
_emitLocal('close', event);
_handleDisconnect(event);
});
} catch (error) {
_handleDisconnect(error);
}
}