From 1f1c72e3243689be3a90307a5400e104e25befe2 Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 19 Jan 2021 15:47:49 +0800 Subject: [PATCH] feat: support non peer id. --- .../Internal/Connection/LCConnection.cs | 9 +++++++- .../Controller/LCIMSessionController.cs | 23 ++++++++----------- Realtime/Realtime/LCIMClient.cs | 4 ++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Realtime/Realtime/Internal/Connection/LCConnection.cs b/Realtime/Realtime/Internal/Connection/LCConnection.cs index 6073502..8e3d1d5 100644 --- a/Realtime/Realtime/Internal/Connection/LCConnection.cs +++ b/Realtime/Realtime/Internal/Connection/LCConnection.cs @@ -63,6 +63,8 @@ namespace LeanCloud.Realtime.Internal.Connection { // 共享这条连接的 IM Client private readonly Dictionary idToClients; + // 默认 Client id + private string defaultClientId; internal LCConnection(string id) { this.id = id; @@ -149,6 +151,7 @@ namespace LeanCloud.Realtime.Internal.Connection { } private void Disconnect() { + defaultClientId = null; state = State.Closed; heartBeat.Stop(); _ = ws.Close(); @@ -189,7 +192,8 @@ namespace LeanCloud.Realtime.Internal.Connection { Reset(); } else { // 通知 - if (idToClients.TryGetValue(command.PeerId, out LCIMClient client)) { + string peerId = command.HasPeerId ? command.PeerId : defaultClientId; + if (idToClients.TryGetValue(peerId, out LCIMClient client)) { // 通知具体客户端 client.HandleNotification(command); } @@ -260,6 +264,9 @@ namespace LeanCloud.Realtime.Internal.Connection { internal void Register(LCIMClient client) { idToClients[client.Id] = client; + if (defaultClientId == null) { + defaultClientId = client.Id; + } } internal void UnRegister(LCIMClient client) { diff --git a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs index 8fd3072..b70235d 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs @@ -21,7 +21,7 @@ namespace LeanCloud.Realtime.Internal.Controller { SessionCommand session = await NewSessionCommand(); session.R = !force; - session.ConfigBitmap = 0x2B; + session.ConfigBitmap = 0xAB; GenericCommand request = NewCommand(CommandType.Session, OpType.Open); request.SessionMessage = session; GenericCommand response = await Connection.SendRequest(request); @@ -37,8 +37,11 @@ namespace LeanCloud.Realtime.Internal.Controller { GenericCommand response = await Connection.SendRequest(request); if (response.Op == OpType.Opened) { UpdateSession(response.SessionMessage); + Connection.Register(Client); } else if (response.Op == OpType.Closed) { - OnClosed(response.SessionMessage); + Connection.UnRegister(Client); + SessionCommand command = response.SessionMessage; + throw new LCException(command.Code, command.Reason); } } @@ -113,23 +116,17 @@ namespace LeanCloud.Realtime.Internal.Controller { internal override void HandleNotification(GenericCommand notification) { switch (notification.Op) { - case OpType.Closed: - OnClosed(notification.SessionMessage); + case OpType.Closed: { + Connection.UnRegister(Client); + SessionCommand command = notification.SessionMessage; + Client.OnClose(command.Code, command.Reason); + } break; default: break; } } - - private void OnClosed(SessionCommand session) { - int code = session.Code; - string reason = session.Reason; - string detail = session.Detail; - Connection.UnRegister(Client); - Client.OnClose?.Invoke(code, reason); - } - #endregion } } diff --git a/Realtime/Realtime/LCIMClient.cs b/Realtime/Realtime/LCIMClient.cs index a7ef349..88cf67a 100644 --- a/Realtime/Realtime/LCIMClient.cs +++ b/Realtime/Realtime/LCIMClient.cs @@ -452,10 +452,10 @@ namespace LeanCloud.Realtime { await SessionController.Reopen(); // 回调用户 OnResume?.Invoke(); - } catch (Exception e) { + } catch (LCException e) { LCLogger.Error(e); // 重连成功,但 session/open 失败 - OnClose?.Invoke(0, string.Empty); + OnClose?.Invoke(e.Code, e.Message); } }