chore: 相同 app 共享连接

oneRain 2020-06-22 18:13:26 +08:00
parent 371f604a79
commit 5891f9287b
3 changed files with 89 additions and 10 deletions

View File

@ -12,6 +12,28 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// 连接层,只与数据协议相关 /// 连接层,只与数据协议相关
/// </summary> /// </summary>
public class LCConnection { public class LCConnection {
/// <summary>
/// 连接状态
/// </summary>
enum State {
/// <summary>
/// 初始状态
/// </summary>
None,
/// <summary>
/// 连接中
/// </summary>
Connecting,
/// <summary>
/// 连接成功
/// </summary>
Open,
/// <summary>
/// 关闭的
/// </summary>
Closed,
}
/// <summary> /// <summary>
/// 发送超时 /// 发送超时
/// </summary> /// </summary>
@ -62,6 +84,9 @@ namespace LeanCloud.Realtime.Internal.Connection {
private LCWebSocketClient client; private LCWebSocketClient client;
private State state;
private Task connectTask;
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>>();
@ -71,9 +96,22 @@ namespace LeanCloud.Realtime.Internal.Connection {
OnMessage = OnClientMessage, OnMessage = OnClientMessage,
OnClose = OnClientDisconnect OnClose = OnClientDisconnect
}; };
state = State.None;
} }
internal async Task Connect() { internal Task Connect() {
if (state == State.Open) {
return Task.FromResult<object>(null);
}
if (state == State.Connecting) {
return connectTask;
}
connectTask = _Connect();
return connectTask;
}
internal async Task _Connect() {
state = State.Connecting;
try { try {
LCRTMServer rtmServer = await router.GetServer(); LCRTMServer rtmServer = await router.GetServer();
try { try {
@ -86,6 +124,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
} }
// 启动心跳 // 启动心跳
heartBeat.Start(); heartBeat.Start();
state = State.Open;
} catch (Exception e) { } catch (Exception e) {
throw e; throw e;
} }
@ -191,14 +230,16 @@ namespace LeanCloud.Realtime.Internal.Connection {
} }
private void OnClientDisconnect() { private void OnClientDisconnect() {
state = State.Closed;
heartBeat.Stop(); heartBeat.Stop();
OnDisconnect?.Invoke(); OnDisconnect?.Invoke();
// 重连 // 重连
_ = Reconnect(); _ = Reconnect();
} }
private async void OnPingTimeout() { private void OnPingTimeout() {
await client.Close(); state = State.Closed;
_ = client.Close();
OnDisconnect?.Invoke(); OnDisconnect?.Invoke();
// 重连 // 重连
_ = Reconnect(); _ = Reconnect();

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Linq; using System.Linq;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using LeanCloud.Common;
using LeanCloud.Storage; using LeanCloud.Storage;
using LeanCloud.Realtime.Internal.Protocol; using LeanCloud.Realtime.Internal.Protocol;
using LeanCloud.Realtime.Internal.Controller; using LeanCloud.Realtime.Internal.Controller;
@ -283,11 +282,10 @@ namespace LeanCloud.Realtime {
MessageController = new LCIMMessageController(this); MessageController = new LCIMMessageController(this);
GoAwayController = new LCIMGoAwayController(this); GoAwayController = new LCIMGoAwayController(this);
Connection = new LCConnection(Id) { Connection = LCRealtime.GetConnection(LCApplication.AppId);
OnNotification = OnConnectionNotification, Connection.OnNotification = OnConnectionNotification;
OnDisconnect = OnConnectionDisconnect, Connection.OnDisconnect = OnConnectionDisconnect;
OnReconnected = OnConnectionReconnect Connection.OnReconnected = OnConnectionReconnect;
};
} }
/// <summary> /// <summary>
@ -315,7 +313,7 @@ namespace LeanCloud.Realtime {
public async Task Close() { public async Task Close() {
// 关闭 session // 关闭 session
await SessionController.Close(); await SessionController.Close();
await Connection.Close(); //await Connection.Close();
} }
/// <summary> /// <summary>

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using LeanCloud.Realtime.Internal.Connection;
namespace LeanCloud.Realtime {
public class LCRealtime {
/// <summary>
/// RTM 服务中,每个 app 对应一条连接
/// </summary>
private static readonly Dictionary<string, LCConnection> appToConnections = new Dictionary<string, LCConnection>();
/// <summary>
/// 获取对应的 Connection
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
internal static LCConnection GetConnection(string appId) {
if (appToConnections.TryGetValue(appId, out LCConnection connection)) {
return connection;
}
string connId = appId.Substring(0, 8).ToLower();
connection = new LCConnection(connId);
appToConnections[appId] = connection;
return connection;
}
/// <summary>
/// 主动断开所有 RTM 连接
/// </summary>
public static void Pause() {
}
/// <summary>
/// 主动恢复所有 RTM 连接
/// </summary>
public static void Resume() {
}
}
}