修改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/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);
}
}
}

Loading…
Cancel
Save