SocketMsgReceiveCenter.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 专门接受消息的模块
  6. /// </summary>
  7. public class SocketMsgReceiveCenter
  8. {
  9. public SocketMsgReceiveCenter
  10. ()
  11. {
  12. }
  13. private bool isTrying;//是否正在尝试连接
  14. private bool isConnected;//是否建立了连接
  15. private SocketMsgSendManager sendManager;
  16. public void Init()
  17. {
  18. Engine.Net.NetWorkManager.GetInstance().AddNetMessageEvent(SocketMsgCallBack);
  19. }
  20. public void Dispose()
  21. {
  22. Engine.Net.NetWorkManager.GetInstance().DelNetMessageEvent(SocketMsgCallBack);
  23. if (GamePlayerData.Instance.room_data.CurRoomId != -1)
  24. {
  25. PlayerEnterRoom(GamePlayerData.Instance.room_data.CurRoomId, false);
  26. }
  27. DisConnect();
  28. }
  29. //连接Socket
  30. public void StartConnect()
  31. {
  32. isTrying = true;
  33. if (isConnected)
  34. {
  35. Engine.Net.NetWorkManager.GetInstance().DelSocket();
  36. }
  37. IsWorking = true;
  38. Engine.Net.NetWorkManager.GetInstance().CreateSocket(GameServerInfo.Instance.CurServer.ipStr, GameServerInfo.Instance.CurServer.port);
  39. }
  40. //主动断开
  41. public void StopConnect()
  42. {
  43. if (!isConnected)
  44. {
  45. return;
  46. }
  47. Engine.Net.NetWorkManager.GetInstance().DelSocket();
  48. isTrying = false;
  49. IsWorking = false;
  50. }
  51. //断线后 尝试恢复原有的连接
  52. public void ReConnect()
  53. {
  54. if(isTrying)
  55. {
  56. return;
  57. }
  58. CDebug.Log("ReConnect");
  59. isTrying = true;
  60. Engine.Net.NetWorkManager.GetInstance().ReConnectSocket(GameServerInfo.Instance.CurServer.ipStr, GameServerInfo.Instance.CurServer.port);
  61. }
  62. //断开连接
  63. public void DisConnect()
  64. {
  65. if (!isConnected)
  66. {
  67. return;
  68. }
  69. isTrying = false;
  70. CDebug.Log("主动断开连接");
  71. Engine.Net.NetWorkManager.GetInstance().OnDestroy();
  72. }
  73. private bool SocketMsgCallBack(Engine.Net.NetMsg iMsg)
  74. {
  75. //CDebug.Log("有消息接受" + iMsg.nCode);
  76. switch(iMsg.nCode)
  77. {
  78. case Engine.Net.NetCode.MSG_CONE_CONNECT_FAIL:
  79. isConnected = false;
  80. isTrying = false;
  81. CDebug.Log("Socket 连接失败");
  82. GameNetManager.Instance.BadNet();
  83. break;
  84. case Engine.Net.NetCode.MSG_CONE_CONNECT_SUCCESS:
  85. isConnected = true;
  86. isTrying = false;
  87. CDebug.Log("Socket 连接成功");
  88. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA);
  89. break;
  90. case MsgConst.MSG_CTS_ROOM_DATA_BACK:
  91. GamePlayerData.Instance.room_data.SetRoomId(iMsg);
  92. //拿到房号后再获取大厅列表的消息
  93. //ExperimentUIManager.Instance.ShowReConnect();//
  94. SendGetDatingData();
  95. break;
  96. case MsgConst.MSG_CTS_DATING_ENTER_BACK:
  97. GamePlayerData.Instance.room_data.InitData(iMsg);
  98. //打开大厅界面 显示房间列表
  99. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_POP_BY_TYPE, new ShowPopData(PopType.RoomList, null));
  100. //检查断线重连
  101. if (GamePlayerData.Instance.room_data.NeedReconnect())
  102. {
  103. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_RECONNECT_POP);
  104. }
  105. break;
  106. case MsgConst.MSG_CTS_DATING_LOGOUT_BACK:
  107. GamePlayerData.Instance.user_id = -1;
  108. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_LOGOUT);
  109. break;
  110. case MsgConst.MSG_CTS_ROOM_CREATE_BACK:
  111. CDebug.Log("同步新房间的出现的消息");
  112. GamePlayerData.Instance.room_data.AddRommData(iMsg);
  113. break;
  114. case MsgConst.MSG_CTS_ROOM_REFRESH_BACK:
  115. GamePlayerData.Instance.room_data.UpdateRoomData(iMsg);
  116. break;
  117. case MsgConst.MSG_CTS_ROOM_USER_ENTER_OR_OUT_BACK:
  118. CDebug.Log("用户进出房间的消息");
  119. GamePlayerData.Instance.room_data.UpdateUser(iMsg);
  120. break;
  121. case MsgConst.MSG_CTS_ROOM_DELETE_BACK:
  122. CDebug.Log("房间删除了");
  123. GamePlayerData.Instance.room_data.RemoveRoomData(iMsg);
  124. break;
  125. case MsgConst.MSG_CTS_RECONNECT_GET_STUDENT_LIST_BACK:
  126. GamePlayerData.Instance.room_data.InitUserList(iMsg);
  127. break;
  128. case MsgConst.MSG_CTS_RECONNECT_GET_RELOAD_BACK:
  129. CDebug.Log("重置课程的进度");
  130. break;
  131. case MsgConst.MSG_CTS_TEACHER_CORE_DATA_BACK:
  132. GamePlayerData.Instance.teacher_operate_data.AcceptOperateMsg(iMsg);
  133. break;
  134. case MsgConst.MSG_CTS_LINE_DATA_BACK:
  135. GamePlayerData.Instance.teacher_operate_data.RefreshPos(iMsg);
  136. break;
  137. case MsgConst.MSG_CTS_AUDIO_DATA_BACK:
  138. GamePlayerData.Instance.teacher_operate_data.RefreshAudioData(iMsg);
  139. break;
  140. case MsgConst.MSG_CTS_SEND_DATA_TO_BACK:
  141. StudentTaskReceiveManager.Instance.AcceptStudentMsg(iMsg);
  142. break;
  143. case MsgConst.MSG_CTS_REFRESH_DATA_BACK:
  144. GamePlayerData.Instance.teacher_operate_data.AcceptRefreshMsg(iMsg);
  145. break;
  146. case MsgConst.MSG_CTS_SEND_CHANGE_MASTER_DATA_TO_TEACHER_BACK:
  147. CDebug.Log("收到了换房的消息了");
  148. GamePlayerData.Instance.room_data.ChangeOwnerId(iMsg);
  149. break;
  150. case 9999:
  151. CDebug.Log("测试消息的返回");
  152. GamePlayerData.Instance.room_data.TestData(iMsg);
  153. break;
  154. }
  155. return true;
  156. }
  157. //发送某个用户进入/退出了某个房间 is_enter flase表示退出了房间
  158. public void PlayerEnterRoom(short room_id, bool is_enter)
  159. {
  160. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_ENTER_OR_OUT_SEND);
  161. UserEnterData data = new UserEnterData();
  162. data.room_id = room_id;
  163. data.is_enter = is_enter;
  164. sendMsg.PackMsg(data);
  165. Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id);
  166. CDebug.Log("Socket 进出房间的消息" + is_enter +" 房间号 " + room_id);
  167. if(!is_enter)
  168. {
  169. //当前的房间销毁了
  170. GamePlayerData.Instance.room_data.CurRoomId = -1;
  171. }
  172. else
  173. {
  174. GamePlayerData.Instance.room_data.CurRoomId = room_id;
  175. }
  176. }
  177. public void SendGetDatingData()
  178. {
  179. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_DATING_ENTER_SEND);
  180. Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id);
  181. CDebug.Log("Socket 请求房间列表的的消息" + sendMsg.UserId);
  182. }
  183. //获取房间里的用户列表
  184. public void GetRoomStudentDatas()
  185. {
  186. return;//不需要拿是直接返回的
  187. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_RECONNECT_GET_STUDENT_LIST_BACK);
  188. Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id);
  189. CDebug.Log("Socket 请求房间里用户列表的的消息" + sendMsg.UserId);
  190. }
  191. public bool IsConnecting
  192. {
  193. get { return isConnected; }
  194. }
  195. public bool IsTrying { get { return isTrying; } }
  196. private bool isWorking;
  197. public bool IsWorking {
  198. get { return isWorking; }
  199. set {
  200. isWorking = value;
  201. }
  202. }
  203. }