ClientReconnectingState.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. //客户端断线重连
  6. public class ClientReconnectingState : ClientConnectingState
  7. {
  8. Coroutine m_ReconnectCoroutine;
  9. int curAttempts;
  10. //断线重连间隔
  11. const float TimeBetweenAttempts = 5;
  12. public override void Enter()
  13. {
  14. curAttempts = 0;
  15. m_ReconnectCoroutine = m_ConnectionManager.NetworkManager.StartCoroutine(ReconnectCoroutine());
  16. //通知UI,转Loading
  17. MessageCenter.SendMessage(EventDefine.ConnectStatusEvent, ConnectStatus.Reconnecting);
  18. //MessageCenter.SendMessage(EventDefine.ClientReConnectionEvent, new ReconnectMessage(curAttempts, m_ConnectionManager.ReconnectAttempts));
  19. }
  20. public override void Exit()
  21. {
  22. if(m_ReconnectCoroutine != null)
  23. {
  24. m_ConnectionManager.NetworkManager.StopCoroutine(m_ReconnectCoroutine);
  25. m_ReconnectCoroutine = null;
  26. }
  27. //触发消息
  28. //MessageCenter.SendMessage(EventDefine.ClientReConnectionEvent, new ReconnectMessage(curAttempts, m_ConnectionManager.ReconnectAttempts));
  29. }
  30. //连接成功,跳转到ClientConnectedState
  31. public override void OnClientConnected(ulong clientId)
  32. {
  33. MessageCenter.SendMessage(EventDefine.ConnectStatusEvent, ConnectStatus.Success);
  34. m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnected);
  35. }
  36. //服务端被动断开连接
  37. public override void OnClientDisconnect(ulong _)
  38. {
  39. if(curAttempts < m_ConnectionManager.ReconnectAttempts)
  40. {
  41. //次数没到。继续重连
  42. m_ReconnectCoroutine = m_ConnectionManager.NetworkManager.StartCoroutine(ReconnectCoroutine());
  43. }
  44. else
  45. {
  46. //次数到了,还是连不上,跳转到OfflineState
  47. MessageCenter.SendMessage(EventDefine.ConnectStatusEvent, ConnectStatus.GenericDisconnect);
  48. m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
  49. }
  50. }
  51. //服务端主动断开连接。切换到DisconnectingWithReasonState。异常处理
  52. public override void OnDisconnectReasonReceived(ConnectStatus disconnectReason)
  53. {
  54. MessageCenter.SendMessage(EventDefine.ConnectStatusEvent, disconnectReason);
  55. //MessageCenter.SendMessage(EventDefine.ClientDisConnectionEvent, disconnectReason);
  56. switch(disconnectReason)
  57. {
  58. case ConnectStatus.UserRequestedDisconnect:
  59. case ConnectStatus.HostEndedSession:
  60. case ConnectStatus.ServerFull:
  61. m_ConnectionManager.ChangeState(m_ConnectionManager.m_DisconnectingWithReason);
  62. break;
  63. }
  64. }
  65. //开启断线重连
  66. IEnumerator ReconnectCoroutine()
  67. {
  68. //如果不是第一次重连,则等待一段时间(当问题是临时的,可以预留一些时间让其自修复)。也可以用Exponential Backoff方式来处理
  69. if(curAttempts > 0)
  70. {
  71. yield return new WaitForSeconds(TimeBetweenAttempts);
  72. }
  73. Debug.Log("Lost connection to host, trying to reconnect...");
  74. //先关闭
  75. m_ConnectionManager.NetworkManager.Shutdown();
  76. //等待直到NetworkManager完全关闭
  77. yield return new WaitWhile(() => m_ConnectionManager.NetworkManager.ShutdownInProgress);
  78. //重连次数++
  79. curAttempts++;
  80. Debug.Log($"Reconnecting attempt {curAttempts}/{m_ConnectionManager.ReconnectAttempts}...");
  81. //触发消息
  82. //MessageCenter.SendMessage(EventDefine.ConnectStatusEvent, ConnectStatus.Reconnecting);
  83. //MessageCenter.SendMessage(EventDefine.ClientReConnectionEvent, new ReconnectMessage(curAttempts, m_ConnectionManager.ReconnectAttempts));
  84. //开始连接
  85. Task connectingClient = ConnectClientAsync();
  86. yield return new WaitUntil(() => connectingClient.IsCompleted);
  87. }
  88. }