chore: 相同 app 共享连接
parent
371f604a79
commit
5891f9287b
|
@ -12,6 +12,28 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
|||
/// 连接层,只与数据协议相关
|
||||
/// </summary>
|
||||
public class LCConnection {
|
||||
/// <summary>
|
||||
/// 连接状态
|
||||
/// </summary>
|
||||
enum State {
|
||||
/// <summary>
|
||||
/// 初始状态
|
||||
/// </summary>
|
||||
None,
|
||||
/// <summary>
|
||||
/// 连接中
|
||||
/// </summary>
|
||||
Connecting,
|
||||
/// <summary>
|
||||
/// 连接成功
|
||||
/// </summary>
|
||||
Open,
|
||||
/// <summary>
|
||||
/// 关闭的
|
||||
/// </summary>
|
||||
Closed,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送超时
|
||||
/// </summary>
|
||||
|
@ -62,6 +84,9 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
|||
|
||||
private LCWebSocketClient client;
|
||||
|
||||
private State state;
|
||||
private Task connectTask;
|
||||
|
||||
internal LCConnection(string id) {
|
||||
this.id = id;
|
||||
responses = new Dictionary<int, TaskCompletionSource<GenericCommand>>();
|
||||
|
@ -71,9 +96,22 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
|||
OnMessage = OnClientMessage,
|
||||
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 {
|
||||
LCRTMServer rtmServer = await router.GetServer();
|
||||
try {
|
||||
|
@ -86,6 +124,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
|||
}
|
||||
// 启动心跳
|
||||
heartBeat.Start();
|
||||
state = State.Open;
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
|
@ -191,14 +230,16 @@ namespace LeanCloud.Realtime.Internal.Connection {
|
|||
}
|
||||
|
||||
private void OnClientDisconnect() {
|
||||
state = State.Closed;
|
||||
heartBeat.Stop();
|
||||
OnDisconnect?.Invoke();
|
||||
// 重连
|
||||
_ = Reconnect();
|
||||
}
|
||||
|
||||
private async void OnPingTimeout() {
|
||||
await client.Close();
|
||||
private void OnPingTimeout() {
|
||||
state = State.Closed;
|
||||
_ = client.Close();
|
||||
OnDisconnect?.Invoke();
|
||||
// 重连
|
||||
_ = Reconnect();
|
||||
|
|
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using System.Collections.ObjectModel;
|
||||
using LeanCloud.Common;
|
||||
using LeanCloud.Storage;
|
||||
using LeanCloud.Realtime.Internal.Protocol;
|
||||
using LeanCloud.Realtime.Internal.Controller;
|
||||
|
@ -283,11 +282,10 @@ namespace LeanCloud.Realtime {
|
|||
MessageController = new LCIMMessageController(this);
|
||||
GoAwayController = new LCIMGoAwayController(this);
|
||||
|
||||
Connection = new LCConnection(Id) {
|
||||
OnNotification = OnConnectionNotification,
|
||||
OnDisconnect = OnConnectionDisconnect,
|
||||
OnReconnected = OnConnectionReconnect
|
||||
};
|
||||
Connection = LCRealtime.GetConnection(LCApplication.AppId);
|
||||
Connection.OnNotification = OnConnectionNotification;
|
||||
Connection.OnDisconnect = OnConnectionDisconnect;
|
||||
Connection.OnReconnected = OnConnectionReconnect;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -315,7 +313,7 @@ namespace LeanCloud.Realtime {
|
|||
public async Task Close() {
|
||||
// 关闭 session
|
||||
await SessionController.Close();
|
||||
await Connection.Close();
|
||||
//await Connection.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue