修改websocket服务器地址

添加控制台打印屏蔽
hmxc
张萌 3 years ago
parent 59544d4747
commit 58f4e0fcce

@ -4,7 +4,7 @@ import 'package:power_logger/power_logger.dart';
import 'package:web_socket_channel/io.dart'; import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.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 { enum SOCKETSTATUS {
CONNECTED, // CONNECTED, //
BREAKOFF, // BREAKOFF, //
@ -22,7 +22,7 @@ class WebSocketUtil {
IOWebSocketChannel? _webSocket; IOWebSocketChannel? _webSocket;
/// ///
String user = 'admin'; String _user = 'admin';
/// ///
SOCKETSTATUS _socketStatus = SOCKETSTATUS.CLOSED; SOCKETSTATUS _socketStatus = SOCKETSTATUS.CLOSED;
@ -54,9 +54,13 @@ class WebSocketUtil {
/// ///
Function? onClosed; Function? onClosed;
///
bool _consolePrint = false;
///websocket ///websocket
void initWebSocket( void initWebSocket(
{Duration? heartDuration, {Duration? heartDuration,
bool? consolePrint,
Function? onStart, Function? onStart,
Function(String message)? onReceiveMes, Function(String message)? onReceiveMes,
Function? onClosed, Function? onClosed,
@ -65,6 +69,9 @@ class WebSocketUtil {
this.onReceiveMes = onReceiveMes; this.onReceiveMes = onReceiveMes;
this.onClosed = onClosed; this.onClosed = onClosed;
this.onError = onError; this.onError = onError;
if (consolePrint != null) {
this._consolePrint = consolePrint;
}
if (heartDuration != null) { if (heartDuration != null) {
this._heartDuration = heartDuration; this._heartDuration = heartDuration;
} }
@ -73,15 +80,15 @@ class WebSocketUtil {
/// ///
void setUser(String user) { void setUser(String user) {
this.user = user; this._user = user;
} }
///websocket ///websocket
void startWebSocket() { void startWebSocket() {
closeWebSocket(); closeWebSocket();
try { try {
_webSocket = IOWebSocketChannel.connect(Uri.parse('$baseUri/$user')); _webSocket = IOWebSocketChannel.connect(Uri.parse('$baseUri/$_user'));
print('webSocket已连接服务器$baseUri/$user'); print('webSocket已连接服务器$baseUri/$_user');
_socketStatus = SOCKETSTATUS.CONNECTED; _socketStatus = SOCKETSTATUS.CONNECTED;
endReconnect(); endReconnect();
onStart?.call(); onStart?.call();
@ -100,7 +107,7 @@ class WebSocketUtil {
// //
webSocketReceiveMessage(message) { webSocketReceiveMessage(message) {
if (message == '心跳正常') { if (message == '心跳正常') {
print('心跳正常————————${DateTime.now()}'); _dPrint('心跳正常————————${DateTime.now()}');
} else { } else {
onReceiveMes?.call(message); onReceiveMes?.call(message);
} }
@ -111,6 +118,7 @@ class WebSocketUtil {
closeWebSocket(); closeWebSocket();
onClosed?.call(); onClosed?.call();
} }
// //
webSocketOnError(e) { webSocketOnError(e) {
WebSocketChannelException ex = e; WebSocketChannelException ex = e;
@ -160,7 +168,6 @@ class WebSocketUtil {
_heartTimer = null; _heartTimer = null;
} }
///
///websocket ///websocket
void closeWebSocket() { void closeWebSocket() {
if (_webSocket != null) { if (_webSocket != null) {
@ -177,7 +184,7 @@ class WebSocketUtil {
if (_webSocket != null) { if (_webSocket != null) {
switch (_socketStatus) { switch (_socketStatus) {
case SOCKETSTATUS.CONNECTED: case SOCKETSTATUS.CONNECTED:
print('发送中:' + message); _dPrint('发送中:' + message);
_webSocket!.sink.add(message); _webSocket!.sink.add(message);
break; break;
case SOCKETSTATUS.CLOSED: case SOCKETSTATUS.CLOSED:
@ -191,4 +198,11 @@ class WebSocketUtil {
} }
} }
} }
//print
void _dPrint(dynamic data) {
if (!this._consolePrint) {
print(data);
}
}
} }

Loading…
Cancel
Save