From 6def41745a8b9acb0db1314564a0160c31acc077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=88=E6=A3=AE=E5=90=B4?= <8402134@qq.com> Date: Wed, 1 Feb 2023 14:18:07 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=8C=E5=96=84=E9=BB=91=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=82=80=E8=AF=B7=E5=A5=BD=E5=8F=8B=E7=94=B3?= =?UTF-8?q?=E8=AF=B7=E5=A4=84=E7=90=86=202.=E5=AE=8C=E5=96=84=E4=BB=A3?= =?UTF-8?q?=E8=BF=90=E8=90=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 应用/MessageSupervises/Class1.cs | 12 +++- 类库/Api.Framework/NoticeSocketClient.cs | 80 ++++++++++++++++++++---- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/应用/MessageSupervises/Class1.cs b/应用/MessageSupervises/Class1.cs index 388a023..e090092 100644 --- a/应用/MessageSupervises/Class1.cs +++ b/应用/MessageSupervises/Class1.cs @@ -168,7 +168,17 @@ namespace MessageSupervises private void SDK_WXApplyFriendEvent(object sender, Chat.Framework.WXSdk.Events.WXApplyFriend e) { - e.Cancel = _ForbidBlacklist(e.NewFriendWxid, e.NewFriendNick, $"申请添加好友", e.Client); + var isCancel = _ForbidBlacklist(e.NewFriendWxid, e.NewFriendNick, $"申请添加好友", e.Client); + + if (isCancel == false) + { + //查看邀请用户是否黑名单 + if (!string.IsNullOrWhiteSpace(e.OldFriendWxid)) + { + isCancel = _ForbidBlacklist(e.OldFriendWxid, e.OldFriendNick, $"邀请好友,申请添加", e.Client); + } + } + e.Cancel = isCancel; } private void SDK_WXApplyGroupEvent(object sender, Chat.Framework.WXSdk.Events.WXApplyGroup e) diff --git a/类库/Api.Framework/NoticeSocketClient.cs b/类库/Api.Framework/NoticeSocketClient.cs index 9fa26e8..b64db5f 100644 --- a/类库/Api.Framework/NoticeSocketClient.cs +++ b/类库/Api.Framework/NoticeSocketClient.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using WebSocket = WebSocketSharp.WebSocket; using CsharpHttpHelper; +using WebSocketSharp; namespace Api.Framework { @@ -75,6 +76,8 @@ namespace Api.Framework /// public bool IsConnected => (_websocket != null && _websocket.IsAlive); + private string ClientId { get; set; } = Guid.NewGuid().ToString("N"); + /// /// 连接 /// @@ -87,19 +90,38 @@ namespace Api.Framework _websocket?.Close(); } - _websocket = new WebSocket($"ws://{this._host}/api/push?id={DateTime.Now.Ticks}"); + _websocket = new WebSocket($"ws://{this._host}/api/push?id={ClientId}"); _websocket.OnOpen += _websocket_OnOpen; + _websocket.OnError += _websocket_OnError; + _websocket.OnClose += _websocket_OnClose; _websocket.OnMessage += _websocket_OnMessage; _websocket.Connect(); - - if (_thread == null) + _pingId = Guid.NewGuid().ToString("N"); + _thread = new Thread(Ping) { - _thread = new Thread(Ping) - { - IsBackground = true - }; - _thread.Start(); - } + IsBackground = true + }; + _thread.Start(); + } + /// + /// 连接断开 + /// + /// + /// + private void _websocket_OnClose(object sender, CloseEventArgs e) + { + //断开自动重连 + this.Connect(); + } + /// + /// 出现连接错误 + /// + /// + /// + private void _websocket_OnError(object sender, ErrorEventArgs e) + { + //断开自动重连 + this.Connect(); } /// @@ -153,18 +175,50 @@ namespace Api.Framework Console.WriteLine($"即时通讯API连接异常:{ex.Message}"); } } + + private int PingFailureCount { get; set; } + private string _pingId = Guid.NewGuid().ToString(); private void Ping() { - while (true) + var id = Guid.NewGuid().ToString(); + _pingId = id; + while (_pingId == id) { Thread.Sleep(1000 * 10); try { //测试ping是否正常 - if (this._websocket.Ping()) continue; + try + { + if (_websocket == null) + { + continue; + } + if (_websocket.ReadyState == WebSocketState.Open) + { + _websocket.Send("\u0001\u0002"); + this.PingFailureCount = 0; + + } + else + { + this.PingFailureCount = this.PingFailureCount + 1; + } + } + catch + { + // ignored + this.PingFailureCount = this.PingFailureCount + 1; + } + finally + { + if (this.PingFailureCount > 5) + { + Console.WriteLine("心跳异常连接断开"); + _websocket?.Close(); + } + } - //断开自动重连 - this.Connect(); } catch (Exception e) {