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