chore: 支持主动断开和重连

oneRain 2020-06-24 12:15:19 +08:00
parent b353bf5536
commit e30d528416
3 changed files with 79 additions and 45 deletions

View File

@ -114,29 +114,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
heartBeat.Start();
state = State.Open;
} catch (Exception e) {
state = State.Closed;
throw e;
}
}
/// <summary>
/// 重置连接
/// </summary>
/// <returns></returns>
internal async Task Reset() {
state = State.Closed;
heartBeat?.Stop();
// 关闭就连接
await ws.Close();
// 重新创建连接组件
heartBeat = new LCHeartBeat(this, OnPingTimeout);
router = new LCRTMRouter();
ws = new LCWebSocketClient {
OnMessage = OnClientMessage,
OnClose = OnClientDisconnect
};
await Reconnect();
}
/// <summary>
/// 发送请求,会在收到应答后返回
/// </summary>
@ -171,15 +153,21 @@ namespace LeanCloud.Realtime.Internal.Connection {
}
/// <summary>
/// 关闭连接
/// 断开连接
/// </summary>
/// <returns></returns>
internal async Task Close() {
LCRealtime.RemoveConnection(this);
private void Disconnect() {
state = State.Closed;
heartBeat.Stop();
await ws.Close();
_ = ws.Close();
foreach (LCIMClient client in idToClients.Values) {
client.HandleDisconnected();
}
}
/// <summary>
/// 消息接收回调
/// </summary>
/// <param name="bytes"></param>
private void OnClientMessage(byte[] bytes) {
try {
GenericCommand command = GenericCommand.Parser.ParseFrom(bytes);
@ -207,7 +195,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
if (command.Cmd == CommandType.Echo) {
heartBeat.Pong();
} else if (command.Cmd == CommandType.Goaway) {
_ = Reset();
Reset();
} else {
// 通知
if (idToClients.TryGetValue(command.PeerId, out LCIMClient client)) {
@ -221,23 +209,35 @@ namespace LeanCloud.Realtime.Internal.Connection {
}
}
/// <summary>
/// 连接断开回调
/// </summary>
private void OnClientDisconnect() {
state = State.Closed;
heartBeat.Stop();
foreach (LCIMClient client in idToClients.Values) {
client.HandleDisconnected();
}
// 重连
Disconnect();
_ = Reconnect();
}
/// <summary>
/// Pong 超时回调
/// </summary>
private void OnPingTimeout() {
state = State.Closed;
_ = ws.Close();
foreach (LCIMClient client in idToClients.Values) {
client.HandleDisconnected();
Disconnect();
_ = Reconnect();
}
// 重连
/// <summary>
/// 重置连接
/// </summary>
/// <returns></returns>
internal void Reset() {
Disconnect();
// 重新创建连接组件
heartBeat = new LCHeartBeat(this, OnPingTimeout);
router = new LCRTMRouter();
ws = new LCWebSocketClient {
OnMessage = OnClientMessage,
OnClose = OnClientDisconnect
};
_ = Reconnect();
}
@ -289,7 +289,8 @@ namespace LeanCloud.Realtime.Internal.Connection {
internal void UnRegister(LCIMClient client) {
idToClients.Remove(client.Id);
if (idToClients.Count == 0) {
_ = Close();
Disconnect();
LCRealtime.RemoveConnection(this);
}
}
@ -297,14 +298,14 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// 暂停连接
/// </summary>
internal void Pause() {
Disconnect();
}
/// <summary>
/// 恢复连接
/// </summary>
internal void Resume() {
_ = Reconnect();
}
}
}

View File

@ -34,14 +34,18 @@ namespace LeanCloud.Realtime {
/// 主动断开所有 RTM 连接
/// </summary>
public static void Pause() {
foreach (LCConnection connection in appToConnections.Values) {
connection.Pause();
}
}
/// <summary>
/// 主动恢复所有 RTM 连接
/// </summary>
public static void Resume() {
foreach (LCConnection connection in appToConnections.Values) {
connection.Resume();
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Realtime;
@ -7,15 +8,43 @@ using static System.Console;
namespace RealtimeApp {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
WriteLine("Hello World!");
SingleThreadSynchronizationContext.Run(async () => {
LCLogger.LogDelegate += Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
LCIMClient client = new LCIMClient("lean");
LCIMClient client = new LCIMClient("lean") {
OnPaused = () => {
WriteLine("~~~~~~~~~~~~~~~ disconnected");
},
OnResume = () => {
WriteLine("~~~~~~~~~~~~~~~ reconnected");
}
};
await client.Open();
//await client.Close();
int count = 0;
while (count < 2) {
WriteLine($"pause : {count}");
await Task.Delay(5 * 1000);
LCRealtime.Pause();
await Task.Delay(5 * 1000);
LCRealtime.Resume();
await Task.Delay(5 * 1000);
count++;
}
try {
await client.Close();
// Done
} catch (Exception e) {
WriteLine($"xxxxxxxxxxxx {e.Message}");
}
});
}