Merge remote-tracking branch 'origin/ios_nearby' into dev_cyp

This commit is contained in:
CaiYanPeng 2021-11-23 09:34:20 +08:00
commit 6cc2486ebf
5 changed files with 73 additions and 13 deletions

View File

@ -100,6 +100,7 @@ public static class App
public static float canvasWidth { get; set; } public static float canvasWidth { get; set; }
public static string WorkoutsUrl { get; internal set; } public static string WorkoutsUrl { get; internal set; }
public static long delayTime { get; set; } public static long delayTime { get; set; }
public static List<OnlineUser> userList = new List<OnlineUser>();
static App() static App()
{ {

View File

@ -13,6 +13,8 @@ namespace Assets.Scenes.Ride.Scripts.Network
void Send(byte[] dgram, int bytes); void Send(byte[] dgram, int bytes);
void Disconnect();
void Close(); void Close();
} }
} }

View File

@ -90,10 +90,19 @@ namespace Assets.Scenes.Ride.Scripts
{ {
var now = DateTime.Now; var now = DateTime.Now;
//超过一秒没有收到消息 //超过一秒没有收到消息
Console.WriteLine($"Heartbeat_Elapsed{(now - LastActiveTime).TotalSeconds}");
if ((now - LastActiveTime).TotalSeconds > 1) if ((now - LastActiveTime).TotalSeconds > 1)
{ {
onlineUserHelper.SetOnlineUser(new List<ReceiveMsgModel>()); onlineUserHelper.SetOnlineUser(new List<ReceiveMsgModel>());
} }
#if UNITY_IOS
//超过5s没收到消息断开重连一次
if ((now - LastActiveTime).TotalSeconds > 5)
{
LastActiveTime = now;
_udpService.Disconnect();
}
#endif
if (now.Second % 20 == 0) if (now.Second % 20 == 0)
{ {
//LastActiveTime = now; //LastActiveTime = now;
@ -386,9 +395,26 @@ namespace Assets.Scenes.Ride.Scripts
var ids = msgs.Where(m => onlineUsers.All(u => u.Id != m.MemberId)).Select(m => m.MemberId); var ids = msgs.Where(m => onlineUsers.All(u => u.Id != m.MemberId)).Select(m => m.MemberId);
if (ids.Any()) if (ids.Any())
{ {
MapApi api = new MapApi(); List<int> notCachedIds = new List<int>();
var rangeUser = api.GetOnlineUserInfo(ids).data; foreach (var userId in ids)
onlineUsers.AddRange(rangeUser); {
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) foreach (var user in onlineUsers)
{ {
@ -429,11 +455,11 @@ namespace Assets.Scenes.Ride.Scripts
} }
onlineUsers.RemoveAll(u => u.IsLost); onlineUsers.RemoveAll(u => u.IsLost);
} }
catch (Exception) catch (Exception ex)
{ {
#if DEBUG Thread.Sleep(3000);
throw; Debug.WriteLine(ex.Message);
#endif //throw;
//return new List<OnlineUser>(); //return new List<OnlineUser>();
} }
} }

View File

@ -11,6 +11,7 @@ using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NetCoreServer;
namespace Assets.Scenes.Ride.Scripts.Network namespace Assets.Scenes.Ride.Scripts.Network
{ {
@ -47,15 +48,41 @@ namespace Assets.Scenes.Ride.Scripts.Network
//_tcpClient.WriteLine(System.Text.Encoding.ASCII.GetString(dgram)); //_tcpClient.WriteLine(System.Text.Encoding.ASCII.GetString(dgram));
var txt = System.Text.Encoding.ASCII.GetString(dgram); var txt = System.Text.Encoding.ASCII.GetString(dgram);
//Debug.WriteLine("发送:"+txt); //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() public void Close()
{ {
isExit = true; try
_tcpClient.DisconnectAndStop(); {
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() protected override void OnDisconnected()
{ {
//base.OnDisconnected(); //base.OnDisconnected();
Debug.WriteLine("tcp断线3秒后重连"); Console.WriteLine("tcp断线3秒后重连");
Thread.Sleep(3000); Thread.Sleep(3000);
if (!_stop) if (!_stop)
@ -172,10 +199,10 @@ namespace Assets.Scenes.Ride.Scripts.Network
//Debug.WriteLine("收到:" + returnData+"\r\n"); //Debug.WriteLine("收到:" + returnData+"\r\n");
//System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "接收到的数据.txt", returnData.Trim().Replace("\0", "")+"\r\n", Encoding.UTF8); //System.IO.File.AppendAllText(System.Environment.CurrentDirectory + "接收到的数据.txt", returnData.Trim().Replace("\0", "")+"\r\n", Encoding.UTF8);
} }
protected override void OnError(SocketError error) protected override void OnError(SocketError error)
{ {
//base.OnError(error); //base.OnError(error);
Console.WriteLine("出错后重启");
Debug.WriteLine(error); Debug.WriteLine(error);
} }
} }

View File

@ -84,6 +84,10 @@ namespace Assets.Scenes.Ride.Scripts.Network
udpClient?.Send(dgram, bytes); udpClient?.Send(dgram, bytes);
} }
public void Disconnect()
{
}
public void Close() public void Close()
{ {
isExit = true; isExit = true;