using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 专门发消息的模块 /// public class SocketMsgSendManager { public void Init() { CDebug.Log("SocketMsgSendManager Init"); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA, SendPlayerData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CREATE_ROOM_SOCKET_DATA, SendCreateRoomData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_STUDENT_SELECT_SOCKET_DATA, SendStudentCoreOperateData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_TEACHER_LINE_POS_SOCKET_DATA, SendTeacherLinePosData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_MSG_TO_NEW_STUDENT, RefreshTeacherCurData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CHANGE_MASTER_SOCKET_DATA, SendChangeRoomOwnerClick); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_CREATE_PUBLIC_ROOM_SOCKET_DATA, SendCreatePublicRoomData); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_AUDIO_DATA, SendAudio); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, SendTeacherDrawClick); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, SendTeacherAudioChange); MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_MR_CLICK_DATA, MRBtnClick); //UI同步的新逻辑实现 MessageCenterController.Instance.Register(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, SendUIChange); } public void Dispose() { MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA, SendPlayerData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CREATE_ROOM_SOCKET_DATA, SendCreateRoomData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_STUDENT_SELECT_SOCKET_DATA, SendStudentCoreOperateData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_TEACHER_LINE_POS_SOCKET_DATA, SendTeacherLinePosData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_MSG_TO_NEW_STUDENT, RefreshTeacherCurData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CHANGE_MASTER_SOCKET_DATA, SendChangeRoomOwnerClick); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_CREATE_PUBLIC_ROOM_SOCKET_DATA, SendCreatePublicRoomData); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_AUDIO_DATA, SendAudio); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_DRAW_SOCKET_DATA, SendTeacherDrawClick); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_AUDIO_SOCKET_DATA, SendTeacherAudioChange); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_MR_CLICK_DATA, MRBtnClick); //UI同步的新逻辑实现 MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SEND_UI_CHANGE_DATA, SendUIChange); } //向服务器发送个人信息 private void SendPlayerData(System.Object datas) { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_USER_DATA_SEND); StartConnectData data = new StartConnectData(); data.user_name = GamePlayerData.Instance.user_name; var len = System.Text.Encoding.UTF8.GetByteCount(data.user_name); //CDebug.Log("名字的长度 " + len); data.user_type = short.Parse(GamePlayerData.Instance.user_type.ToString()); data.school_id = short.Parse(GamePlayerData.Instance.school_id.ToString()); //data.device_id = SystemInfo.deviceUniqueIdentifier;//设备号 CDebug.Log("个人信息 用户类型 " + data.user_type); sendMsg.PackMsg(data); SendMsg(sendMsg); //IvChatManager.Instance.ConnectToHost(GamePlayerData.Instance.user_name, GamePlayerData.Instance.user_name); } //发送创建房间的数据 private void SendCreateRoomData(System.Object datas) { InitRoomData(); SocketRoomItemData data = datas as SocketRoomItemData; Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_CREATE_SEND); sendMsg.PackMsg(data); SendMsg(sendMsg); } private void SendCreatePublicRoomData(System.Object datas) { InitRoomData(); CourseConfigData c_data = (CourseConfigData)GameConfigData.Instance.course_data.GetList()[0]; SocketRoomItemData data = new SocketRoomItemData(); data.course_name = c_data.course_name; //data.course_name = "四地5G公开课"; data.subject_id = short.Parse(c_data.subject_id); data.course_id = short.Parse(c_data.id); data.is_public = true; data.owner_id = GamePlayerData.Instance.user_id; data.owner_name = GamePlayerData.Instance.user_name; Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_CREATE_SEND); sendMsg.PackMsg(data); SendMsg(sendMsg); } //初始化房间的数据记录 private void InitRoomData() { } //发送老师的手柄指示的信息 public void SendTeacherLinePosData(System.Object datas) { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_LINE_DATA_TO_STUDENT); MoveData data = new MoveData(); data.pos = (Vector3)datas; sendMsg.PackMsg(data); SendMsg(sendMsg); } public void SendStudentCoreOperateData(System.Object datas) { QuestionImageView item = datas as QuestionImageView; SelectData data = new SelectData(); data.index = short.Parse(item.ConfigId); data.res = (short)item.SelectIndex; data.costTime = item.CostTime; data.timeStamp = CStaticMethod.CurTimeStamp(); CDebug.Log("我提交的答案是" + data.res); //本地也进行记录 StudentTaskReceiveManager.Instance.AddLocalTask(data, UserReport.ReportType.Default); //SendCoreData(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data })); SendMessageToAll(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data })); } /* public void SendStudentScoreData(System.Object datas) { ScoreData data = datas as ScoreData; SendCoreData(BrocastClassType.StudentScore, data); } */ private void SendMessageToAll(BrocastClassType mType, System.Object o_data) { if(o_data == null) { CDebug.Log(mType.ToString() + "严重错误"); return; } Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_TEACHER_CORE_DATA_SEND); BrocastClass data = new BrocastClass(); data.mType = (short)mType; sendMsg.PackMsg(data); sendMsg.PackMsg(o_data); SendMsg(sendMsg); //CDebug.Log("广播 " + mType.ToString() + " --- " + sendMsg.UserId); } //发送给老师的 private void SendCoreData(BrocastClassType mType, System.Object o_data) { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_DATA_TO_TEACHER); BrocastClass data = new BrocastClass(); data.mType = (short)mType; sendMsg.PackMsg(data); sendMsg.PackMsg(o_data); SendMsg(sendMsg); CDebug.Log("学生发送 Socket " + mType.ToString() + " --- " + sendMsg.UserId); } private void SendMsg(Engine.Net.NetMsg sendMsg) { Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id); } /// /// 发送给学生 告知我当前所处的状态 /// private void RefreshTeacherCurData(System.Object o_data) { int ccc_id = (int)o_data;//学生的ID号 CDebug.Log("同步给学生的UIChange数据" + ccc_id); //告诉这个学生当前的UI信息 SendTeacherToStudentById(BrocastClassType.UIChange, UIDataManager.Instance.GetCurAllUIDataToStudent(), ccc_id); //告诉他 语音消息 //SendTeacherToStudentById(BrocastClassType.Audio, AudioDataManager.Instance.CurAudioData, ccc_id); } //老法师发送给单个学生的信息 private void SendTeacherToStudentById(BrocastClassType mType, System.Object o_data, int targetId) { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_REFRESH_DATA_TO_STUDENT); BrocastClass data = new BrocastClass(); data.mType = (short)mType; sendMsg.PackMsg(data); sendMsg.PackMsg(o_data); Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, targetId); } //更换房主 private void SendChangeRoomOwnerClick(System.Object datas) { ChangeOwner data = new ChangeOwner(); UserData user_data = datas as UserData; data.owner_id = user_data.user_id; data.room_id = GamePlayerData.Instance.room_data.CurRoomId;//当前的房间号 Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_CHANGE_MASTER_DATA_TO_TEACHER); sendMsg.PackMsg(data); SendMsg(sendMsg); CDebug.Log("发送换房 Socket " + data.owner_id); } //发送语音包 private void SendAudio(System.Object datas) { VoiceChatPacket data = datas as VoiceChatPacket; Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_SEND_AUDIO_DATA_TO_STUDENT); sendMsg.PackMsg(data); SendMsg(sendMsg); } private void SendTeacherDrawClick(System.Object datas) { SendMessageToAll(BrocastClassType.DrawData, FengzhuangCoreData(new object[1] { datas })); } private void SendTeacherAudioChange(System.Object datas) { CDebug.Log("发送 SendTeacherAudioChange"); SendMessageToAll(BrocastClassType.Audio, datas); } //MR游戏里的点击事件发送 这里是 protected virtual void MRBtnClick(System.Object datas) { MRData data = datas as MRData; if(data.test == (int)MRGameMode.Refresh) { //SendCoreData(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data })); SendMessageToAll(BrocastClassType.CoreData, FengzhuangCoreData(new object[1] { data })); return; } CDebug.Log("这里有重大错误出现"); //SendTeacherToStudent(BrocastClassType.MR, datas); //MessageCenter.Instance.Broadcast(GameEnum.MESSAGE_MR_PLAY_DATA, data); } protected virtual void SendUIChange(System.Object datas) { SocketUIChangeData data = datas as SocketUIChangeData; SendMessageToAll(BrocastClassType.UIChange, datas); } private SocketCoreData FengzhuangCoreData(System.Object[] datas) { SocketCoreData send_data = new SocketCoreData(); send_data.ct = (short)datas.Length; send_data.classes = new SocketClassData[send_data.ct]; send_data.datas = datas; for (int i = 0; i< send_data.ct; i++) { send_data.classes[i] = new SocketClassData(); send_data.classes[i].DataClassName = (datas[i].GetType()).ToString(); } return send_data; } /* protected virtual void SendCoreData(System.Object datas) { SocketCoreData data = datas as SocketCoreData; SendMessageToAll(BrocastClassType.CoreData, datas); } */ }