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)
{