* LCConnection.cs:

* LCHeartBeat.cs: chore: 关闭连接时停止心跳检测
oneRain 2020-04-30 13:34:41 +08:00
parent c08873f1ab
commit 08449aea6b
5 changed files with 64 additions and 3 deletions

View File

@ -21,7 +21,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// <summary>
/// 最大重连次数,超过后重置 Router 缓存后再次尝试重连
/// </summary>
private const int MAX_RECONNECT_TIMES = 3;
private const int MAX_RECONNECT_TIMES = 10;
/// <summary>
/// 重连间隔
@ -31,7 +31,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// <summary>
/// 心跳间隔
/// </summary>
private const int HEART_BEAT_INTERVAL = 5000;
private const int HEART_BEAT_INTERVAL = 30000;
/// <summary>
/// 通知事件
@ -148,6 +148,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
OnNotification = null;
OnDisconnect = null;
OnReconnected = null;
heartBeat.Stop();
await client.Close();
}

View File

@ -39,7 +39,7 @@ namespace LeanCloud.Realtime.Internal.Connection {
/// </summary>
/// <returns></returns>
internal async Task Refresh(Action onTimeout) {
LCLogger.Debug("HeartBeat update");
LCLogger.Debug("HeartBeat refresh");
pingCTS?.Cancel();
pongCTS?.Cancel();

View File

@ -0,0 +1,9 @@
using System;
namespace RealtimeApp {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,43 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace LeanCloud.Common {
/// <summary>
/// 单线程环境,用于控制台应用 await 返回
/// </summary>
public class SingleThreadSynchronizationContext : SynchronizationContext {
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
public override void Post(SendOrPostCallback d, object state) {
queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
}
public void RunOnCurrentThread() {
while (queue.TryTake(out KeyValuePair<SendOrPostCallback, object> workItem, Timeout.Infinite)) {
workItem.Key(workItem.Value);
}
}
public void Complete() {
queue.CompleteAdding();
}
public static void Run(Func<Task> func) {
SynchronizationContext prevContext = Current;
try {
SingleThreadSynchronizationContext syncContext = new SingleThreadSynchronizationContext();
SetSynchronizationContext(syncContext);
Task t = func();
syncContext.RunOnCurrentThread();
t.GetAwaiter().GetResult();
} finally {
SetSynchronizationContext(prevContext);
}
}
}
}