Implementation
Future<void> connect(String url) async {
if (url.length < 10) {
logger.e('Socket url is too short');
throw WebSocketError(message: 'Invalid URL supplied');
}
// already conncted on same url
if (_connected && url == _url) return;
var hdrs = <String, dynamic>{};
hdrs['Content-Type'] = 'application/json;charset=utf8';
try {
_socket = WebSocketChannel.connect(Uri.parse(url));
_disconnect = false;
_connected = true;
_url = url;
_startPing();
_functionConnect();
} catch (e) {
logger.e('Websocket connection error: ' + e.toString());
throw WebSocketError(message: e.toString());
}
_rx = _socket!.stream;
_subscription = _rx?.listen(
onReceiveData,
onError: onReceiveError,
onDone: onReceiveDone,
cancelOnError: true,
);
}