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 string WorkoutsUrl { get; internal set; }
public static long delayTime { get; set; }
public static List<OnlineUser> userList = new List<OnlineUser>();
static App()
{

View File

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

View File

@ -90,10 +90,19 @@ 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>());
}
#if UNITY_IOS
//超过5s没收到消息断开重连一次
if ((now - LastActiveTime).TotalSeconds > 5)
{
LastActiveTime = now;
_udpService.Disconnect();
}
#endif
if (now.Second % 20 == 0)
{
//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);
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 +455,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>();
}
}

View File

@ -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);
}
}

View File

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