Implementation
Hotline(this.url, {required this.onConnect, required this.onDisconnect, this.headers, this.protocols, this.pingInterval, this.onConnectionRefused}) {
socketChannel = IOWebSocketChannel.connect(url);
connectionState = HotlineSocketConnectionState(onConnect: _onConnect, onDisconnect: _onDisconnect);
connectionState.stateType = HotlineSocketConnectionType.connecting;
stream = socketChannel.stream.listen((data) {
final payload = jsonDecode(data);
if(payload['type'] != null) {
// if the payload has a type attribute we can assume it's a protocol message
_dispatchProtocolMessage(payload);
}else {
// handle the broadcast....
_dispatchDataMessage(payload);
}
}, onError: (_) {
_onDisconnect();
});
// initialise the subscription manager for this connection
subscriptions = HotlineSubscriptionManager(this);
}