init method
void
init({})
Creates and connects the socket to url.
handshakeEvent: event emitted right after connecting, with the payload from SmartApiSocketHooks.buildHandshakePayload. Leave null if your server doesn't need a post-connect handshake.handshakeAckEvent: event the server emits back to acknowledge the handshake. Defaults to the same name ashandshakeEvent.transports,reconnectionAttempts,reconnectionDelay: passed straight through tosocket_io_client'sOptionBuilder.extraHeaders: extra headers/options for the underlying engine.io connection (defaults to{'forceNew': 'true'}, matching typical Flutter socket setups). Values must beStrings.listenToConnectivity: when true (default), the socket auto-reconnects as soon asconnectivity_plusreports the network is back, and isConnected/isHandshakeComplete are reset to false while offline.
Implementation
void init({
required String url,
String? handshakeEvent,
String? handshakeAckEvent,
List<String> transports = const ['websocket'],
int reconnectionAttempts = 15,
int reconnectionDelay = 2000,
Map<String, String>? extraHeaders,
bool listenToConnectivity = true,
}) {
_handshakeEvent = handshakeEvent;
_handshakeAckEvent = handshakeAckEvent ?? handshakeEvent;
_socket = io.io(
url,
io.OptionBuilder()
.setTransports(transports)
.enableAutoConnect()
.setReconnectionAttempts(reconnectionAttempts)
.setReconnectionDelay(reconnectionDelay)
.setExtraHeaders(extraHeaders ?? {'forceNew': 'true'})
.build(),
);
_bindCoreListeners();
if (listenToConnectivity) _startListeningToNetwork();
}