Merge pull request #98 from onerain88/peer-id

feat: support non peer id.
oneRain 2021-01-19 16:00:16 +08:00 committed by GitHub
commit c45b15a387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 16 deletions

View File

@ -63,6 +63,8 @@ namespace LeanCloud.Realtime.Internal.Connection {
// 共享这条连接的 IM Client // 共享这条连接的 IM Client
private readonly Dictionary<string, LCIMClient> idToClients; private readonly Dictionary<string, LCIMClient> idToClients;
// 默认 Client id
private string defaultClientId;
internal LCConnection(string id) { internal LCConnection(string id) {
this.id = id; this.id = id;
@ -149,6 +151,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
} }
private void Disconnect() { private void Disconnect() {
defaultClientId = null;
state = State.Closed; state = State.Closed;
heartBeat.Stop(); heartBeat.Stop();
_ = ws.Close(); _ = ws.Close();
@ -189,7 +192,8 @@ namespace LeanCloud.Realtime.Internal.Connection {
Reset(); Reset();
} else { } 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); client.HandleNotification(command);
} }
@ -260,6 +264,9 @@ namespace LeanCloud.Realtime.Internal.Connection {
internal void Register(LCIMClient client) { internal void Register(LCIMClient client) {
idToClients[client.Id] = client; idToClients[client.Id] = client;
if (defaultClientId == null) {
defaultClientId = client.Id;
}
} }
internal void UnRegister(LCIMClient client) { internal void UnRegister(LCIMClient client) {

View File

@ -21,7 +21,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
SessionCommand session = await NewSessionCommand(); SessionCommand session = await NewSessionCommand();
session.R = !force; session.R = !force;
session.ConfigBitmap = 0x2B; session.ConfigBitmap = 0xAB;
GenericCommand request = NewCommand(CommandType.Session, OpType.Open); GenericCommand request = NewCommand(CommandType.Session, OpType.Open);
request.SessionMessage = session; request.SessionMessage = session;
GenericCommand response = await Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
@ -37,8 +37,11 @@ namespace LeanCloud.Realtime.Internal.Controller {
GenericCommand response = await Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
if (response.Op == OpType.Opened) { if (response.Op == OpType.Opened) {
UpdateSession(response.SessionMessage); UpdateSession(response.SessionMessage);
Connection.Register(Client);
} else if (response.Op == OpType.Closed) { } 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) { internal override void HandleNotification(GenericCommand notification) {
switch (notification.Op) { switch (notification.Op) {
case OpType.Closed: case OpType.Closed: {
OnClosed(notification.SessionMessage); Connection.UnRegister(Client);
SessionCommand command = notification.SessionMessage;
Client.OnClose(command.Code, command.Reason);
}
break; break;
default: default:
break; 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 #endregion
} }
} }

View File

@ -452,10 +452,10 @@ namespace LeanCloud.Realtime {
await SessionController.Reopen(); await SessionController.Reopen();
// 回调用户 // 回调用户
OnResume?.Invoke(); OnResume?.Invoke();
} catch (Exception e) { } catch (LCException e) {
LCLogger.Error(e); LCLogger.Error(e);
// 重连成功,但 session/open 失败 // 重连成功,但 session/open 失败
OnClose?.Invoke(0, string.Empty); OnClose?.Invoke(e.Code, e.Message);
} }
} }