diff --git a/Realtime/Realtime/Internal/Connection/LCConnection.cs b/Realtime/Realtime/Internal/Connection/LCConnection.cs index 2f11347..db350b7 100644 --- a/Realtime/Realtime/Internal/Connection/LCConnection.cs +++ b/Realtime/Realtime/Internal/Connection/LCConnection.cs @@ -114,29 +114,11 @@ namespace LeanCloud.Realtime.Internal.Connection { heartBeat.Start(); state = State.Open; } catch (Exception e) { + state = State.Closed; throw e; } } - /// - /// 重置连接 - /// - /// - 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(); - } - /// /// 发送请求,会在收到应答后返回 /// @@ -171,15 +153,21 @@ namespace LeanCloud.Realtime.Internal.Connection { } /// - /// 关闭连接 + /// 断开连接 /// - /// - 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(); + } } + /// + /// 消息接收回调 + /// + /// 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 { } } + /// + /// 连接断开回调 + /// private void OnClientDisconnect() { - state = State.Closed; - heartBeat.Stop(); - foreach (LCIMClient client in idToClients.Values) { - client.HandleDisconnected(); - } - // 重连 + Disconnect(); _ = Reconnect(); } + /// + /// Pong 超时回调 + /// private void OnPingTimeout() { - state = State.Closed; - _ = ws.Close(); - foreach (LCIMClient client in idToClients.Values) { - client.HandleDisconnected(); - } - // 重连 + Disconnect(); + _ = Reconnect(); + } + + /// + /// 重置连接 + /// + /// + 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 { /// 暂停连接 /// internal void Pause() { - + Disconnect(); } /// /// 恢复连接 /// internal void Resume() { - + _ = Reconnect(); } } } diff --git a/Realtime/Realtime/LCRealtime.cs b/Realtime/Realtime/LCRealtime.cs index 6f579a4..612650e 100644 --- a/Realtime/Realtime/LCRealtime.cs +++ b/Realtime/Realtime/LCRealtime.cs @@ -34,14 +34,18 @@ namespace LeanCloud.Realtime { /// 主动断开所有 RTM 连接 /// public static void Pause() { - + foreach (LCConnection connection in appToConnections.Values) { + connection.Pause(); + } } /// /// 主动恢复所有 RTM 连接 /// public static void Resume() { - + foreach (LCConnection connection in appToConnections.Values) { + connection.Resume(); + } } } } diff --git a/Sample/RealtimeApp/Program.cs b/Sample/RealtimeApp/Program.cs index 01ef348..1680362 100644 --- a/Sample/RealtimeApp/Program.cs +++ b/Sample/RealtimeApp/Program.cs @@ -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}"); + } }); }