SocketMsgSendManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 专门发消息的模块
  6. /// </summary>
  7. public class SocketMsgSendManager {
  8. public void Init()
  9. {
  10. CDebug.Log("SocketMsgSendManager Init");
  11. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA, SendPlayerData);
  12. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CREATE_ROOM_SOCKET_DATA, SendCreateRoomData);
  13. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_STUDENT_SELECT_SOCKET_DATA, SendStudentCoreOperateData);
  14. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_TEACHER_LINE_POS_SOCKET_DATA, SendTeacherLinePosData);
  15. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_MSG_TO_NEW_STUDENT, RefreshTeacherCurData);
  16. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CHANGE_MASTER_SOCKET_DATA, SendChangeRoomOwnerClick);
  17. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CREATE_PUBLIC_ROOM_SOCKET_DATA, SendCreatePublicRoomData);
  18. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_AUDIO_DATA, SendAudio);
  19. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, SendTeacherDrawClick);
  20. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, SendTeacherAudioChange);
  21. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_MR_CLICK_DATA, MRBtnClick);
  22. //UI同步的新逻辑实现
  23. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, SendUIChange);
  24. }
  25. public void Dispose()
  26. {
  27. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA, SendPlayerData);
  28. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CREATE_ROOM_SOCKET_DATA, SendCreateRoomData);
  29. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_STUDENT_SELECT_SOCKET_DATA, SendStudentCoreOperateData);
  30. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_TEACHER_LINE_POS_SOCKET_DATA, SendTeacherLinePosData);
  31. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_MSG_TO_NEW_STUDENT, RefreshTeacherCurData);
  32. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CHANGE_MASTER_SOCKET_DATA, SendChangeRoomOwnerClick);
  33. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CREATE_PUBLIC_ROOM_SOCKET_DATA, SendCreatePublicRoomData);
  34. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_AUDIO_DATA, SendAudio);
  35. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, SendTeacherDrawClick);
  36. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, SendTeacherAudioChange);
  37. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_MR_CLICK_DATA, MRBtnClick);
  38. //UI同步的新逻辑实现
  39. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, SendUIChange);
  40. }
  41. //向服务器发送个人信息
  42. private void SendPlayerData(System.Object datas)
  43. {
  44. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_USER_DATA_SEND);
  45. StartConnectData data = new StartConnectData();
  46. data.user_name = GamePlayerData.Instance.user_name;
  47. var len = System.Text.Encoding.UTF8.GetByteCount(data.user_name);
  48. //CDebug.Log("名字的长度 " + len);
  49. data.user_type = short.Parse(GamePlayerData.Instance.user_type.ToString());
  50. data.school_id = short.Parse(GamePlayerData.Instance.school_id.ToString());
  51. //data.device_id = SystemInfo.deviceUniqueIdentifier;//设备号
  52. CDebug.Log("个人信息 用户类型 " + data.user_type);
  53. sendMsg.PackMsg(data);
  54. SendMsg(sendMsg);
  55. //IvChatManager.Instance.ConnectToHost(GamePlayerData.Instance.user_name, GamePlayerData.Instance.user_name);
  56. }
  57. //发送创建房间的数据
  58. private void SendCreateRoomData(System.Object datas)
  59. {
  60. InitRoomData();
  61. SocketRoomItemData data = datas as SocketRoomItemData;
  62. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_CREATE_SEND);
  63. sendMsg.PackMsg(data);
  64. SendMsg(sendMsg);
  65. }
  66. private void SendCreatePublicRoomData(System.Object datas)
  67. {
  68. InitRoomData();
  69. CourseConfigData c_data = (CourseConfigData)GameConfigData.Instance.course_data.GetList()[0];
  70. SocketRoomItemData data = new SocketRoomItemData();
  71. data.course_name = c_data.course_name;
  72. //data.course_name = "四地5G公开课";
  73. data.subject_id = short.Parse(c_data.subject_id);
  74. data.course_id = short.Parse(c_data.id);
  75. data.is_public = true;
  76. data.owner_id = GamePlayerData.Instance.user_id;
  77. data.owner_name = GamePlayerData.Instance.user_name;
  78. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_CREATE_SEND);
  79. sendMsg.PackMsg(data);
  80. SendMsg(sendMsg);
  81. }
  82. //初始化房间的数据记录
  83. private void InitRoomData()
  84. {
  85. }
  86. //发送老师的手柄指示的信息
  87. public void SendTeacherLinePosData(System.Object datas)
  88. {
  89. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_LINE_DATA_TO_STUDENT);
  90. MoveData data = new MoveData();
  91. data.pos = (Vector3)datas;
  92. sendMsg.PackMsg(data);
  93. SendMsg(sendMsg);
  94. }
  95. public void SendStudentCoreOperateData(System.Object datas)
  96. {
  97. QuestionImageView item = datas as QuestionImageView;
  98. SelectData data = new SelectData();
  99. data.index = short.Parse(item.ConfigId);
  100. data.res = (short)item.SelectIndex;
  101. data.costTime = item.CostTime;
  102. data.timeStamp = CStaticMethod.CurTimeStamp();
  103. CDebug.Log("我提交的答案是" + data.res);
  104. //本地也进行记录
  105. StudentTaskReceiveManager.Instance.AddLocalTask(data, UserReport.ReportType.Default);
  106. //SendCoreData(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data }));
  107. SendMessageToAll(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data }));
  108. }
  109. /*
  110. public void SendStudentScoreData(System.Object datas)
  111. {
  112. ScoreData data = datas as ScoreData;
  113. SendCoreData(BrocastClassType.StudentScore, data);
  114. }
  115. */
  116. private void SendMessageToAll(BrocastClassType mType, System.Object o_data)
  117. {
  118. if(o_data == null)
  119. {
  120. CDebug.Log(mType.ToString() + "严重错误");
  121. return;
  122. }
  123. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_TEACHER_CORE_DATA_SEND);
  124. BrocastClass data = new BrocastClass();
  125. data.mType = (short)mType;
  126. sendMsg.PackMsg(data);
  127. sendMsg.PackMsg(o_data);
  128. SendMsg(sendMsg);
  129. //CDebug.Log("广播 " + mType.ToString() + " --- " + sendMsg.UserId);
  130. }
  131. //发送给老师的
  132. private void SendCoreData(BrocastClassType mType, System.Object o_data)
  133. {
  134. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_DATA_TO_TEACHER);
  135. BrocastClass data = new BrocastClass();
  136. data.mType = (short)mType;
  137. sendMsg.PackMsg(data);
  138. sendMsg.PackMsg(o_data);
  139. SendMsg(sendMsg);
  140. CDebug.Log("学生发送 Socket " + mType.ToString() + " --- " + sendMsg.UserId);
  141. }
  142. private void SendMsg(Engine.Net.NetMsg sendMsg)
  143. {
  144. Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id);
  145. }
  146. /// <summary>
  147. /// 发送给学生 告知我当前所处的状态
  148. /// </summary>
  149. private void RefreshTeacherCurData(System.Object o_data)
  150. {
  151. int ccc_id = (int)o_data;//学生的ID号
  152. CDebug.Log("同步给学生的UIChange数据" + ccc_id);
  153. //告诉这个学生当前的UI信息
  154. SendTeacherToStudentById(BrocastClassType.UIChange, UIDataManager.Instance.GetCurAllUIDataToStudent(), ccc_id);
  155. //告诉他 语音消息
  156. //SendTeacherToStudentById(BrocastClassType.Audio, AudioDataManager.Instance.CurAudioData, ccc_id);
  157. }
  158. //老法师发送给单个学生的信息
  159. private void SendTeacherToStudentById(BrocastClassType mType, System.Object o_data, int targetId)
  160. {
  161. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_REFRESH_DATA_TO_STUDENT);
  162. BrocastClass data = new BrocastClass();
  163. data.mType = (short)mType;
  164. sendMsg.PackMsg(data);
  165. sendMsg.PackMsg(o_data);
  166. Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, targetId);
  167. }
  168. //更换房主
  169. private void SendChangeRoomOwnerClick(System.Object datas)
  170. {
  171. ChangeOwner data = new ChangeOwner();
  172. UserData user_data = datas as UserData;
  173. data.owner_id = user_data.user_id;
  174. data.room_id = GamePlayerData.Instance.room_data.CurRoomId;//当前的房间号
  175. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_CHANGE_MASTER_DATA_TO_TEACHER);
  176. sendMsg.PackMsg(data);
  177. SendMsg(sendMsg);
  178. CDebug.Log("发送换房 Socket " + data.owner_id);
  179. }
  180. //发送语音包
  181. private void SendAudio(System.Object datas)
  182. {
  183. VoiceChatPacket data = datas as VoiceChatPacket;
  184. Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_AUDIO_DATA_TO_STUDENT);
  185. sendMsg.PackMsg(data);
  186. SendMsg(sendMsg);
  187. }
  188. private void SendTeacherDrawClick(System.Object datas)
  189. {
  190. SendMessageToAll(BrocastClassType.DrawData, FengzhuangCoreData(new object[1] { datas }));
  191. }
  192. private void SendTeacherAudioChange(System.Object datas)
  193. {
  194. CDebug.Log("发送 SendTeacherAudioChange");
  195. SendMessageToAll(BrocastClassType.Audio, datas);
  196. }
  197. //MR游戏里的点击事件发送 这里是
  198. protected virtual void MRBtnClick(System.Object datas)
  199. {
  200. MRData data = datas as MRData;
  201. if(data.test == (int)MRGameMode.Refresh)
  202. {
  203. //SendCoreData(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data }));
  204. SendMessageToAll(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data }));
  205. return;
  206. }
  207. CDebug.Log("这里有重大错误出现");
  208. //SendTeacherToStudent(BrocastClassType.MR, datas);
  209. //MessageCenter.Instance.Broadcast(GameEnum.MESSAGE_MR_PLAY_DATA, data);
  210. }
  211. protected virtual void SendUIChange(System.Object datas)
  212. {
  213. SocketUIChangeData data = datas as SocketUIChangeData;
  214. SendMessageToAll(BrocastClassType.UIChange, datas);
  215. }
  216. private SocketCoreData FengzhuangCoreData(System.Object[] datas)
  217. {
  218. SocketCoreData send_data = new SocketCoreData();
  219. send_data.ct = (short)datas.Length;
  220. send_data.classes = new SocketClassData[send_data.ct];
  221. send_data.datas = datas;
  222. for (int i = 0; i< send_data.ct; i++)
  223. {
  224. send_data.classes[i] = new SocketClassData();
  225. send_data.classes[i].DataClassName = (datas[i].GetType()).ToString();
  226. }
  227. return send_data;
  228. }
  229. /*
  230. protected virtual void SendCoreData(System.Object datas)
  231. {
  232. SocketCoreData data = datas as SocketCoreData;
  233. SendMessageToAll(BrocastClassType.CoreData, datas);
  234. }
  235. */
  236. }