ios附近的人掉线问题
This commit is contained in:
parent
42e23e2f84
commit
d5ea26f15c
@ -100,6 +100,7 @@ public static class App
|
||||
public static float canvasWidth { get; set; }
|
||||
public static string WorkoutsUrl { get; internal set; }
|
||||
public static long delayTime { get; set; }
|
||||
public static List<OnlineUser> userList = new List<OnlineUser>();
|
||||
|
||||
static App()
|
||||
{
|
||||
|
||||
@ -13,6 +13,8 @@ namespace Assets.Scenes.Ride.Scripts.Network
|
||||
|
||||
void Send(byte[] dgram, int bytes);
|
||||
|
||||
void Disconnect();
|
||||
|
||||
void Close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,10 +90,17 @@ namespace Assets.Scenes.Ride.Scripts
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
//超过一秒没有收到消息
|
||||
Console.WriteLine($"Heartbeat_Elapsed{(now - LastActiveTime).TotalSeconds}");
|
||||
if ((now - LastActiveTime).TotalSeconds > 1)
|
||||
{
|
||||
onlineUserHelper.SetOnlineUser(new List<ReceiveMsgModel>());
|
||||
}
|
||||
//超过5s没收到消息断开重连一次
|
||||
if ((now - LastActiveTime).TotalSeconds > 5)
|
||||
{
|
||||
LastActiveTime = now;
|
||||
_udpService.Disconnect();
|
||||
}
|
||||
if (now.Second % 20 == 0)
|
||||
{
|
||||
//LastActiveTime = now;
|
||||
@ -386,9 +393,26 @@ namespace Assets.Scenes.Ride.Scripts
|
||||
var ids = msgs.Where(m => onlineUsers.All(u => u.Id != m.MemberId)).Select(m => m.MemberId);
|
||||
if (ids.Any())
|
||||
{
|
||||
MapApi api = new MapApi();
|
||||
var rangeUser = api.GetOnlineUserInfo(ids).data;
|
||||
onlineUsers.AddRange(rangeUser);
|
||||
List<int> notCachedIds = new List<int>();
|
||||
foreach (var userId in ids)
|
||||
{
|
||||
var user = App.userList.Where(c => c.Id == userId).FirstOrDefault();
|
||||
if (user != null)
|
||||
{
|
||||
onlineUsers.Add(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
notCachedIds.Add(userId);
|
||||
}
|
||||
}
|
||||
|
||||
if (notCachedIds.Count > 0)
|
||||
{
|
||||
var rangeUser = ConfigHelper.mapApi.GetOnlineUserInfo(ids).data;
|
||||
onlineUsers.AddRange(rangeUser);
|
||||
App.userList.AddRange(rangeUser);
|
||||
}
|
||||
}
|
||||
foreach (var user in onlineUsers)
|
||||
{
|
||||
@ -429,11 +453,11 @@ namespace Assets.Scenes.Ride.Scripts
|
||||
}
|
||||
onlineUsers.RemoveAll(u => u.IsLost);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
throw;
|
||||
#endif
|
||||
Thread.Sleep(3000);
|
||||
Debug.WriteLine(ex.Message);
|
||||
//throw;
|
||||
//return new List<OnlineUser>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NetCoreServer;
|
||||
|
||||
namespace Assets.Scenes.Ride.Scripts.Network
|
||||
{
|
||||
@ -47,15 +48,41 @@ namespace Assets.Scenes.Ride.Scripts.Network
|
||||
//_tcpClient.WriteLine(System.Text.Encoding.ASCII.GetString(dgram));
|
||||
var txt = System.Text.Encoding.ASCII.GetString(dgram);
|
||||
//Debug.WriteLine("发送:"+txt);
|
||||
_tcpClient.Send(txt);
|
||||
try
|
||||
{
|
||||
_tcpClient.SendAsync(txt);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
try
|
||||
{
|
||||
_tcpClient.DisconnectAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
isExit = true;
|
||||
_tcpClient.DisconnectAndStop();
|
||||
try
|
||||
{
|
||||
isExit = true;
|
||||
_tcpClient.DisconnectAndStop();
|
||||
|
||||
_tcpClient = null;
|
||||
_tcpClient = null;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -87,7 +114,7 @@ namespace Assets.Scenes.Ride.Scripts.Network
|
||||
protected override void OnDisconnected()
|
||||
{
|
||||
//base.OnDisconnected();
|
||||
Debug.WriteLine("tcp断线,3秒后重连");
|
||||
Console.WriteLine("tcp断线,3秒后重连");
|
||||
Thread.Sleep(3000);
|
||||
|
||||
if (!_stop)
|
||||
@ -172,10 +199,10 @@ namespace Assets.Scenes.Ride.Scripts.Network
|
||||
//Debug.WriteLine("收到:" + returnData+"\r\n");
|
||||
//System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "接收到的数据.txt", returnData.Trim().Replace("\0", "")+"\r\n", Encoding.UTF8);
|
||||
}
|
||||
|
||||
protected override void OnError(SocketError error)
|
||||
{
|
||||
//base.OnError(error);
|
||||
Console.WriteLine("出错后重启");
|
||||
Debug.WriteLine(error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,6 +84,10 @@ namespace Assets.Scenes.Ride.Scripts.Network
|
||||
udpClient?.Send(dgram, bytes);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
isExit = true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user