chore: 简化连接异常处理

oneRain 2020-06-24 12:21:16 +08:00
parent e30d528416
commit c358fd4de9
1 changed files with 10 additions and 18 deletions

View File

@ -77,11 +77,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
internal LCConnection(string id) { internal LCConnection(string id) {
this.id = id; this.id = id;
responses = new Dictionary<int, TaskCompletionSource<GenericCommand>>(); responses = new Dictionary<int, TaskCompletionSource<GenericCommand>>();
heartBeat = new LCHeartBeat(this, OnPingTimeout); heartBeat = new LCHeartBeat(this, OnDisconnect);
router = new LCRTMRouter(); router = new LCRTMRouter();
ws = new LCWebSocketClient { ws = new LCWebSocketClient {
OnMessage = OnClientMessage, OnMessage = OnMessage,
OnClose = OnClientDisconnect OnClose = OnDisconnect
}; };
idToClients = new Dictionary<string, LCIMClient>(); idToClients = new Dictionary<string, LCIMClient>();
state = State.None; state = State.None;
@ -168,7 +168,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// 消息接收回调 /// 消息接收回调
/// </summary> /// </summary>
/// <param name="bytes"></param> /// <param name="bytes"></param>
private void OnClientMessage(byte[] bytes) { private void OnMessage(byte[] bytes) {
try { try {
GenericCommand command = GenericCommand.Parser.ParseFrom(bytes); GenericCommand command = GenericCommand.Parser.ParseFrom(bytes);
LCLogger.Debug($"{id} <= {FormatCommand(command)}"); LCLogger.Debug($"{id} <= {FormatCommand(command)}");
@ -212,15 +212,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// <summary> /// <summary>
/// 连接断开回调 /// 连接断开回调
/// </summary> /// </summary>
private void OnClientDisconnect() { private void OnDisconnect() {
Disconnect();
_ = Reconnect();
}
/// <summary>
/// Pong 超时回调
/// </summary>
private void OnPingTimeout() {
Disconnect(); Disconnect();
_ = Reconnect(); _ = Reconnect();
} }
@ -232,11 +224,11 @@ namespace LeanCloud.Realtime.Internal.Connection {
internal void Reset() { internal void Reset() {
Disconnect(); Disconnect();
// 重新创建连接组件 // 重新创建连接组件
heartBeat = new LCHeartBeat(this, OnPingTimeout); heartBeat = new LCHeartBeat(this, OnDisconnect);
router = new LCRTMRouter(); router = new LCRTMRouter();
ws = new LCWebSocketClient { ws = new LCWebSocketClient {
OnMessage = OnClientMessage, OnMessage = OnMessage,
OnClose = OnClientDisconnect OnClose = OnDisconnect
}; };
_ = Reconnect(); _ = Reconnect();
} }
@ -260,8 +252,8 @@ namespace LeanCloud.Realtime.Internal.Connection {
if (reconnectCount < MAX_RECONNECT_TIMES) { if (reconnectCount < MAX_RECONNECT_TIMES) {
// 重连成功 // 重连成功
LCLogger.Debug("Reconnected"); LCLogger.Debug("Reconnected");
ws.OnMessage = OnClientMessage; ws.OnMessage = OnMessage;
ws.OnClose = OnClientDisconnect; ws.OnClose = OnDisconnect;
foreach (LCIMClient client in idToClients.Values) { foreach (LCIMClient client in idToClients.Values) {
client.HandleReconnected(); client.HandleReconnected();
} }