From 58f4e0fccef88281d1983fe5338d50ae5f2ef233 Mon Sep 17 00:00:00 2001 From: zhangmeng <494089941@qq.com> Date: Fri, 18 Jun 2021 15:40:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9websocket=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=9C=B0=E5=9D=80=20=E6=B7=BB=E5=8A=A0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=8F=B0=E6=89=93=E5=8D=B0=E5=B1=8F=E8=94=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/websocket/web_socket_util.dart | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/utils/websocket/web_socket_util.dart b/lib/utils/websocket/web_socket_util.dart index 4521d14..d483205 100644 --- a/lib/utils/websocket/web_socket_util.dart +++ b/lib/utils/websocket/web_socket_util.dart @@ -4,7 +4,7 @@ import 'package:power_logger/power_logger.dart'; import 'package:web_socket_channel/io.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; -const String baseUri = 'wss://test.kaidalai.cn/websocket'; +const String baseUri = 'wss://test.kaidalai.cn/websocket/butlerApp'; enum SOCKETSTATUS { CONNECTED, //已连接 BREAKOFF, //已断开 @@ -22,7 +22,7 @@ class WebSocketUtil { IOWebSocketChannel? _webSocket; ///用户设置不同的服务器地址 - String user = 'admin'; + String _user = 'admin'; ///连接状态 SOCKETSTATUS _socketStatus = SOCKETSTATUS.CLOSED; @@ -54,9 +54,13 @@ class WebSocketUtil { ///关闭连接回调; Function? onClosed; + ///屏蔽控制台输出 + bool _consolePrint = false; + ///注册websocket void initWebSocket( {Duration? heartDuration, + bool? consolePrint, Function? onStart, Function(String message)? onReceiveMes, Function? onClosed, @@ -65,6 +69,9 @@ class WebSocketUtil { this.onReceiveMes = onReceiveMes; this.onClosed = onClosed; this.onError = onError; + if (consolePrint != null) { + this._consolePrint = consolePrint; + } if (heartDuration != null) { this._heartDuration = heartDuration; } @@ -73,15 +80,15 @@ class WebSocketUtil { ///设置用户 void setUser(String user) { - this.user = user; + this._user = user; } ///开启websocket void startWebSocket() { closeWebSocket(); try { - _webSocket = IOWebSocketChannel.connect(Uri.parse('$baseUri/$user')); - print('webSocket已连接服务器:$baseUri/$user'); + _webSocket = IOWebSocketChannel.connect(Uri.parse('$baseUri/$_user')); + print('webSocket已连接服务器:$baseUri/$_user'); _socketStatus = SOCKETSTATUS.CONNECTED; endReconnect(); onStart?.call(); @@ -100,7 +107,7 @@ class WebSocketUtil { //接收消息回调 webSocketReceiveMessage(message) { if (message == '心跳正常') { - print('心跳正常————————${DateTime.now()}'); + _dPrint('心跳正常————————${DateTime.now()}'); } else { onReceiveMes?.call(message); } @@ -111,6 +118,7 @@ class WebSocketUtil { closeWebSocket(); onClosed?.call(); } + //连接出错回调 webSocketOnError(e) { WebSocketChannelException ex = e; @@ -160,7 +168,6 @@ class WebSocketUtil { _heartTimer = null; } - /// ///关闭websocket void closeWebSocket() { if (_webSocket != null) { @@ -177,7 +184,7 @@ class WebSocketUtil { if (_webSocket != null) { switch (_socketStatus) { case SOCKETSTATUS.CONNECTED: - print('发送中:' + message); + _dPrint('发送中:' + message); _webSocket!.sink.add(message); break; case SOCKETSTATUS.CLOSED: @@ -191,4 +198,11 @@ class WebSocketUtil { } } } + + //封装print + void _dPrint(dynamic data) { + if (!this._consolePrint) { + print(data); + } + } }