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
private readonly Dictionary<string, LCIMClient> 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) {

View File

@ -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
}
}

View File

@ -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);
}
}