断线重连
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DXCLManager : MonoBehaviour {
float lastHeartTime;//收到心跳包的时间
int count;//失联的次数
private void Start()
{
SocketManage.Instance.UpdateMessageUI += DoResponseMessage;
}
private void DoResponseMessage(JsonPacket res)
{
switch (res.MainId)
{
//断线重连
case 0:
switch (res.SubId)
{
case 1:
lastHeartTime = Time.time;
count = 0;
break;
default:
break;
}
break;
default:
break;
}
}
private void Update()
{
if (Time.time - lastHeartTime > 3)//心跳跳动
{
count++;
lastHeartTime = Time.time;
Debug.Log("心跳" + count + "次");
if (count >= 5)
{
Debug.Log("心跳超时5次,断线处理");
SocketManage.Instance.CloseSocket();
}
else
{
JsonPacket pck = new JsonPacket(0, 0, "");
SocketManage.Instance.SendQueue.Enqueue(pck);
}
}
}
}