using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 专门接受消息的模块 /// public class SocketMsgReceiveCenter { public SocketMsgReceiveCenter () { } private bool isTrying;//是否正在尝试连接 private bool isConnected;//是否建立了连接 private SocketMsgSendManager sendManager; public void Init() { Engine.Net.NetWorkManager.GetInstance().AddNetMessageEvent(SocketMsgCallBack); } public void Dispose() { Engine.Net.NetWorkManager.GetInstance().DelNetMessageEvent(SocketMsgCallBack); if (GamePlayerData.Instance.room_data.CurRoomId != -1) { PlayerEnterRoom(GamePlayerData.Instance.room_data.CurRoomId, false); } DisConnect(); } //连接Socket public void StartConnect() { isTrying = true; if (isConnected) { Engine.Net.NetWorkManager.GetInstance().DelSocket(); } IsWorking = true; Engine.Net.NetWorkManager.GetInstance().CreateSocket(GameServerInfo.Instance.CurServer.ipStr, GameServerInfo.Instance.CurServer.port); } //主动断开 public void StopConnect() { if (!isConnected) { return; } Engine.Net.NetWorkManager.GetInstance().DelSocket(); isTrying = false; IsWorking = false; } //断线后 尝试恢复原有的连接 public void ReConnect() { if(isTrying) { return; } CDebug.Log("ReConnect"); isTrying = true; Engine.Net.NetWorkManager.GetInstance().ReConnectSocket(GameServerInfo.Instance.CurServer.ipStr, GameServerInfo.Instance.CurServer.port); } //断开连接 public void DisConnect() { if (!isConnected) { return; } isTrying = false; CDebug.Log("主动断开连接"); Engine.Net.NetWorkManager.GetInstance().OnDestroy(); } private bool SocketMsgCallBack(Engine.Net.NetMsg iMsg) { //CDebug.Log("有消息接受" + iMsg.nCode); switch(iMsg.nCode) { case Engine.Net.NetCode.MSG_CONE_CONNECT_FAIL: isConnected = false; isTrying = false; CDebug.Log("Socket 连接失败"); GameNetManager.Instance.BadNet(); break; case Engine.Net.NetCode.MSG_CONE_CONNECT_SUCCESS: isConnected = true; isTrying = false; CDebug.Log("Socket 连接成功"); MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SEND_PLAYER_SOCKET_DATA); break; case MsgConst.MSG_CTS_ROOM_DATA_BACK: GamePlayerData.Instance.room_data.SetRoomId(iMsg); //拿到房号后再获取大厅列表的消息 //ExperimentUIManager.Instance.ShowReConnect();// SendGetDatingData(); break; case MsgConst.MSG_CTS_DATING_ENTER_BACK: GamePlayerData.Instance.room_data.InitData(iMsg); //打开大厅界面 显示房间列表 MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_POP_BY_TYPE, new ShowPopData(PopType.RoomList, null)); //检查断线重连 if (GamePlayerData.Instance.room_data.NeedReconnect()) { MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_RECONNECT_POP); } break; case MsgConst.MSG_CTS_DATING_LOGOUT_BACK: GamePlayerData.Instance.user_id = -1; MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_LOGOUT); break; case MsgConst.MSG_CTS_ROOM_CREATE_BACK: CDebug.Log("同步新房间的出现的消息"); GamePlayerData.Instance.room_data.AddRommData(iMsg); break; case MsgConst.MSG_CTS_ROOM_REFRESH_BACK: GamePlayerData.Instance.room_data.UpdateRoomData(iMsg); break; case MsgConst.MSG_CTS_ROOM_USER_ENTER_OR_OUT_BACK: CDebug.Log("用户进出房间的消息"); GamePlayerData.Instance.room_data.UpdateUser(iMsg); break; case MsgConst.MSG_CTS_ROOM_DELETE_BACK: CDebug.Log("房间删除了"); GamePlayerData.Instance.room_data.RemoveRoomData(iMsg); break; case MsgConst.MSG_CTS_RECONNECT_GET_STUDENT_LIST_BACK: GamePlayerData.Instance.room_data.InitUserList(iMsg); break; case MsgConst.MSG_CTS_RECONNECT_GET_RELOAD_BACK: CDebug.Log("重置课程的进度"); break; case MsgConst.MSG_CTS_TEACHER_CORE_DATA_BACK: GamePlayerData.Instance.teacher_operate_data.AcceptOperateMsg(iMsg); break; case MsgConst.MSG_CTS_LINE_DATA_BACK: GamePlayerData.Instance.teacher_operate_data.RefreshPos(iMsg); break; case MsgConst.MSG_CTS_AUDIO_DATA_BACK: GamePlayerData.Instance.teacher_operate_data.RefreshAudioData(iMsg); break; case MsgConst.MSG_CTS_SEND_DATA_TO_BACK: StudentTaskReceiveManager.Instance.AcceptStudentMsg(iMsg); break; case MsgConst.MSG_CTS_REFRESH_DATA_BACK: GamePlayerData.Instance.teacher_operate_data.AcceptRefreshMsg(iMsg); break; case MsgConst.MSG_CTS_SEND_CHANGE_MASTER_DATA_TO_TEACHER_BACK: CDebug.Log("收到了换房的消息了"); GamePlayerData.Instance.room_data.ChangeOwnerId(iMsg); break; case 9999: CDebug.Log("测试消息的返回"); GamePlayerData.Instance.room_data.TestData(iMsg); break; } return true; } //发送某个用户进入/退出了某个房间 is_enter flase表示退出了房间 public void PlayerEnterRoom(short room_id, bool is_enter) { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_ROOM_ENTER_OR_OUT_SEND); UserEnterData data = new UserEnterData(); data.room_id = room_id; data.is_enter = is_enter; sendMsg.PackMsg(data); Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id); CDebug.Log("Socket 进出房间的消息" + is_enter +" 房间号 " + room_id); if(!is_enter) { //当前的房间销毁了 GamePlayerData.Instance.room_data.CurRoomId = -1; } else { GamePlayerData.Instance.room_data.CurRoomId = room_id; } } public void SendGetDatingData() { Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_DATING_ENTER_SEND); Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id); CDebug.Log("Socket 请求房间列表的的消息" + sendMsg.UserId); } //获取房间里的用户列表 public void GetRoomStudentDatas() { return;//不需要拿是直接返回的 Engine.Net.NetMsg sendMsg = Engine.Net.NetMsg.OnCreateNetMsg(MsgConst.MSG_CTS_RECONNECT_GET_STUDENT_LIST_BACK); Engine.Net.NetWorkManager.GetInstance().SendMessage(sendMsg, Engine.Net.ESockeType.Game, GamePlayerData.Instance.user_id); CDebug.Log("Socket 请求房间里用户列表的的消息" + sendMsg.UserId); } public bool IsConnecting { get { return isConnected; } } public bool IsTrying { get { return isTrying; } } private bool isWorking; public bool IsWorking { get { return isWorking; } set { isWorking = value; } } }