|
|
@ -1,11 +1,11 @@
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:aku_new_community_manager/tools/user_tool.dart';
|
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
|
|
import 'package:power_logger/power_logger.dart';
|
|
|
|
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://shop.kaidalai.cn/websocket/butlerApp';
|
|
|
|
|
|
|
|
enum SOCKETSTATUS {
|
|
|
|
enum SOCKETSTATUS {
|
|
|
|
CONNECTED, //已连接
|
|
|
|
CONNECTED, //已连接
|
|
|
|
BREAKOFF, //已断开
|
|
|
|
BREAKOFF, //已断开
|
|
|
@ -17,13 +17,18 @@ class WebSocketUtil {
|
|
|
|
|
|
|
|
|
|
|
|
//内部构造函数
|
|
|
|
//内部构造函数
|
|
|
|
WebSocketUtil._();
|
|
|
|
WebSocketUtil._();
|
|
|
|
|
|
|
|
|
|
|
|
//单例模式
|
|
|
|
//单例模式
|
|
|
|
factory WebSocketUtil() => _socket;
|
|
|
|
factory WebSocketUtil() => _socket;
|
|
|
|
|
|
|
|
|
|
|
|
IOWebSocketChannel? _webSocket;
|
|
|
|
IOWebSocketChannel? _webSocket;
|
|
|
|
|
|
|
|
|
|
|
|
///用户设置不同的服务器地址
|
|
|
|
///用户设置不同的服务器地址
|
|
|
|
String _user = 'admin';
|
|
|
|
|
|
|
|
|
|
|
|
static const String baseUri = 'wss://saas.kaidalai.cn/websocket/butlerApp';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String get urlAddress =>
|
|
|
|
|
|
|
|
'$baseUri/${UserTool.userProvider.userInfoModel?.communityName}/${UserTool.userProvider.userInfoModel?.nickName}';
|
|
|
|
|
|
|
|
|
|
|
|
///连接状态
|
|
|
|
///连接状态
|
|
|
|
SOCKETSTATUS _socketStatus = SOCKETSTATUS.CLOSED;
|
|
|
|
SOCKETSTATUS _socketStatus = SOCKETSTATUS.CLOSED;
|
|
|
@ -79,26 +84,23 @@ class WebSocketUtil {
|
|
|
|
print('——————————webSocket init ——————————');
|
|
|
|
print('——————————webSocket init ——————————');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///设置用户
|
|
|
|
|
|
|
|
void setUser(String 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(urlAddress));
|
|
|
|
print('webSocket已连接服务器:$baseUri/$_user');
|
|
|
|
print('webSocket已连接服务器:$urlAddress');
|
|
|
|
_socketStatus = SOCKETSTATUS.CONNECTED;
|
|
|
|
_socketStatus = SOCKETSTATUS.CONNECTED;
|
|
|
|
endReconnect();
|
|
|
|
endReconnect();
|
|
|
|
onStart?.call();
|
|
|
|
onStart?.call();
|
|
|
|
_webSocket!.stream.listen(
|
|
|
|
_webSocket!.stream.listen(
|
|
|
|
(event) => webSocketReceiveMessage(event as String),
|
|
|
|
(event) => webSocketReceiveMessage(event as String),
|
|
|
|
onError: webSocketOnError, );
|
|
|
|
onError: webSocketOnError,
|
|
|
|
|
|
|
|
);
|
|
|
|
initHeartBeat();
|
|
|
|
initHeartBeat();
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
BotToast.showText(text: 'webSocket连接失败');
|
|
|
|
BotToast.showText(text: 'webSocket连接失败');
|
|
|
|
|
|
|
|
print('webSocket连接失败');
|
|
|
|
onError?.call(e);
|
|
|
|
onError?.call(e);
|
|
|
|
LoggerData.addData(e);
|
|
|
|
LoggerData.addData(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|