From c5b447f38ab217d651efeade89c4a44883ac2690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=90=8C?= <494089941@qq.com> Date: Thu, 26 May 2022 17:33:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E5=80=92=E8=AE=A1?= =?UTF-8?q?=E6=97=B6=E9=87=8D=E5=A4=8D=E7=82=B9=E5=87=BBbug=20websocket?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/provider/clock_timer_provider.dart | 7 +++++-- lib/provider/user_provider.dart | 1 - lib/utils/websocket/web_socket_util.dart | 24 +++++++++++++----------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/provider/clock_timer_provider.dart b/lib/provider/clock_timer_provider.dart index aa33dd8..4d6b66e 100644 --- a/lib/provider/clock_timer_provider.dart +++ b/lib/provider/clock_timer_provider.dart @@ -2,7 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; -class ClockTimerProvider extends ChangeNotifier{ +class ClockTimerProvider extends ChangeNotifier { ///登录页验证码计时器 int second = 60; bool timerStart = false; @@ -10,6 +10,9 @@ class ClockTimerProvider extends ChangeNotifier{ void startTimer() { timerStart = true; + if (timer != null) { + return; + } timer = Timer.periodic(Duration(seconds: 1), (timer) { if (second > 0) { second--; @@ -27,4 +30,4 @@ class ClockTimerProvider extends ChangeNotifier{ timer = null; notifyListeners(); } -} \ No newline at end of file +} diff --git a/lib/provider/user_provider.dart b/lib/provider/user_provider.dart index b7ea307..89b1c27 100644 --- a/lib/provider/user_provider.dart +++ b/lib/provider/user_provider.dart @@ -27,7 +27,6 @@ class UserProvider extends ChangeNotifier { await HiveStore.appBox!.put('login', true); if (isLogin) { await updateUserInfo(); - WebSocketUtil().setUser(_userInfoModel!.id.toString()); WebSocketUtil().startWebSocket(); await SignFunc.checkNameAndAccount(); } diff --git a/lib/utils/websocket/web_socket_util.dart b/lib/utils/websocket/web_socket_util.dart index 9175c29..8fc4523 100644 --- a/lib/utils/websocket/web_socket_util.dart +++ b/lib/utils/websocket/web_socket_util.dart @@ -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); }