connect method
Future<void>
connect({
- required String url,
- ConnectError? onError,
- ConnectClose? onClose,
- ConnectOpen? onOpen,
- MessageHandle? onMessage,
Implementation
Future<void> connect(
{required String url,
ConnectError? onError,
ConnectClose? onClose,
ConnectOpen? onOpen,
MessageHandle? onMessage}) async {
_stream = _eventChannel.receiveBroadcastStream().listen((event) {
final json = event as String;
if (json.isNotEmpty) {
final result = socketResultModelFromJson(json);
switch (result.messageType) {
case 'connectError': // 连接失败
if (onError != null) onError(result.data);
break;
case 'connectClose':
final data = result.data!;
final error = socketConnectCloseModelFromJson(data);
if (onClose != null) onClose(error);
break;
case 'connectSuccess':
if (onOpen != null) onOpen(result.data);
break;
case 'connectMessage':
if (onMessage != null) onMessage(result.data);
break;
default:
break;
}
}
});
await FlutterSocket.connect(url: url);
}