/// <summary> /// 发送数据 /// </summary> internal void SendMessage(ClientDataItem item) { if (item != null) { userInfo[item.ip].socket.Send(ToolManager.ObjectToBytes(item.main_CMD, item.sub_CMD, item.data)); } //MessageBox.Show("请选择你要发送的好友!!!"); }
/// <summary> /// 返回房间信息 /// </summary> private void ReturnRoomInfo(int roomNumber, string code, bool isSuccess, ClientDataItem item) { JsonData js = new JsonData { ["roomNumber"] = roomNumber, ["code"] = code, ["isSuccess"] = isSuccess, }; item.data = ToolManager.GetBytes(JsonMapper.ToJson(js)); SendMessage(item); }
/// <summary> /// 接收到消息 /// </summary> private void OnReceiveMessage(MemoryStream ms, int length, string ip) { byte[] data = ms.ToArray(); byte[] mMain = new byte[2]; byte[] mSub = new byte[2]; byte[] mData = new byte[length]; Array.Copy(data, 4, mMain, 0, mMain.Length); Array.Copy(data, 6, mSub, 0, mSub.Length); Array.Copy(data, SIZE_PACK_HEAD, mData, 0, length); ClientDataItem item = new ClientDataItem(); item.main_CMD = GetCMD(mMain); item.sub_CMD = GetCMD(mSub); item.data = mData; item.ip = ip; //string str = Encoding.UTF8.GetString(ms.ToArray()); //Debug.Log("1---接收到的数据:" + str); //AddEvent(allData); HandleData(item);//每个客户端有一个标识 }
/// <summary> /// 处理分配数据------------------------------通过IP Key对象调用各自委托-------------------- /// </summary> private void HandleData(ClientDataItem item) { JsonData js = JsonMapper.ToObject(ToolManager.GetString(item.data)); switch (item.main_CMD) { case 1: int number = RoomNumber(); //房间号 switch (item.sub_CMD) { case 1: //登录 break; case 2: //注册 break; case 3: //根据其中一人创建了房间,创建对象 while (true) { number = RoomNumber(); if (!nius.ContainsKey(number)) { roomKey.Add(item.ip, number); nius.Add(number, new NiuNiu(item)); nius[number].AddPlayerInfo(userInfo[item.ip]); ReturnRoomInfo(number, "创建房间成功", true, item); nius[number].SendAllPlayerInfo(); break; } } break; case 4: // 后面多加一些参数,房间规则等等 number = int.Parse(js["roomNumber"].ToString()); if (nius.ContainsKey(number)) { nius[number].AddPlayerInfo(userInfo[item.ip]); ReturnRoomInfo(number, "加入房间成功", true, item); nius[number].SendAllPlayerInfo(); } else { ReturnRoomInfo(number, "房间号无效", false, item); } break; } //OnLobbyDeleageServer?.Invoke(item);// 处理大厅信息 break; case 100: number = int.Parse(js["roomNumber"].ToString()); nius[number].ReceiveGameData(item); //OnGameLogicDeleage?.Invoke(item);// 处理游戏消息,不做委托有可能触发到所有对象 break; default: var k = js.Keys.GetEnumerator(); while (k.MoveNext()) { ShowMsg(js[k.Current].ToString()); } SendMessage(item); break; } }