修复NetClient的等待主线程的错误
This commit is contained in:
parent
2496964953
commit
de0bae347d
|
@ -76,7 +76,7 @@ namespace BITKit.Net
|
|||
|
||||
private async void ConnectionCallback(bool x)
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
await BITApp.SwitchToMainThread();
|
||||
if (x)
|
||||
{
|
||||
OnConnected?.Invoke();
|
||||
|
@ -102,7 +102,7 @@ namespace BITKit.Net
|
|||
_timer.Stop();
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
||||
await BITApp.SwitchToMainThread();
|
||||
OnDisconnected?.Invoke();
|
||||
}
|
||||
catch (OperationCanceledException){}
|
||||
|
@ -113,37 +113,27 @@ namespace BITKit.Net
|
|||
if (IsConnecting) return false;
|
||||
IsConnecting = true;
|
||||
if (client.connected) return false;
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext, BITApp.CancellationToken);
|
||||
await BITApp.SwitchToMainThread();
|
||||
OnStartConnect?.Invoke();
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
_lastHeartbeat = DateTime.Now;
|
||||
client.Connect(address, port);
|
||||
|
||||
client.Connect(address, port);
|
||||
|
||||
_timer.Start();
|
||||
_interval = TimeSpan.FromMilliseconds(_timer.Interval);
|
||||
// for (var i = 0; i < 5; i++)
|
||||
// {
|
||||
// client.Tick();
|
||||
// await Task.Delay(100);
|
||||
// }
|
||||
|
||||
//_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat });
|
||||
|
||||
//client.Send(new []{(byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
HandShake();
|
||||
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
|
||||
Traffic += new float2(1, 0);
|
||||
//_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat});
|
||||
await BITApp.SwitchToMainThread();
|
||||
|
||||
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
client.Send(new []{(byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
Traffic += new float2(1, 0);
|
||||
client.Tick();
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace BITKit.Net
|
|||
private DateTime _now=DateTime.Now;
|
||||
private TimeSpan _interval=TimeSpan.FromSeconds(0.32);
|
||||
|
||||
private byte[] _heartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
|
||||
private readonly byte[] _heartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
|
||||
|
||||
public KCPNetServer()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue