void GetRoomsInfo() { object[] roomsInfo; string errInfo; if (NetCmdTranslator.Request(out errInfo, out roomsInfo, BR_Common.NetCmd.GetRoomsInfo, null)) { //LobbyList.Clear(); roomGrid.ClearAllChildGO(); print("获取RoomsInfo成功!"); for (int i = 0; i < roomsInfo.Length; i++) { //LobbyList.AddItem(string.Format("Lobby{0}: ({1})", i, (byte)roomsInfo[i])); Transform newRoom = Instantiate(roomTemplete) as Transform; //newRoom.localScale = Vector3.one; newRoom.FindChild("Label").GetComponent <UILabel>().text = string.Format("房间{0} : ({1})", i, roomsInfo[i]); roomGrid.AddChild(newRoom); print("添加新房间号成功!"); } roomGrid.Reposition(); foreach (var item in roomGrid.GetChildList()) { item.localScale = Vector3.one; } } else { Debug.LogError("获取Rooms信息错误:" + errInfo); } }
private void LoginButtonOnClick(GameObject go) { print("LoginButtonOnClick"); if (Check()) { //byte[] echoBytes = udpClientWork.Send( // NetCmdTranslator.Translate( // NetCmd.Login, userNameUIInput.value, passwordUIInput.value // )); object[] responseObjs; bool isSuccessed; string errInfo; isSuccessed = NetCmdTranslator.Request(out errInfo, out responseObjs, NetCmd.Login, new String20(userNameUIInput.value), new String20(passwordUIInput.value)); if (isSuccessed) { foreach (var item in responseObjs) { print("-----" + item.ToString()); } if ((bool)responseObjs[0] == true) { player.playerName = userNameUIInput.value; player.headTextureID = (int)responseObjs[1]; player.grade = (int)responseObjs[2]; player.exp = (int)responseObjs[3]; player.roomOrLobbyIn = (int)responseObjs[4]; player.allLobbyCount = (int)responseObjs[5]; player.lobbyCurrentPlayerCount = (int)responseObjs[6]; //print(AllScene.self.allScene[1].ToString().TrimEnd("(UnityEngine.SceneAsset)".ToCharArray())); //Application.DontDestroyOnLoad(udpClientWork.gameObject); DontDestroyOnLoad(udpClientWork.gameObject); UdpClientWork.self.ChangeNextPort(); Application.LoadLevel("Lobby"); } else { errorLabel.text = (string)responseObjs[1]; } } else { Debug.LogError("接收错误:" + errInfo); } } }
void GetLobbyInfo() { object[] lobbyInfo; string errInfo; if (NetCmdTranslator.Request(out errInfo, out lobbyInfo, BR_Common.NetCmd.GetLobbyInfo, null)) { LobbyList.Clear(); for (int i = 0; i < lobbyInfo.Length; i++) { LobbyList.AddItem(string.Format("Lobby{0}: ({1})", i, (byte)lobbyInfo[i])); } } else { Debug.LogError("获取Lobby信息错误:" + errInfo); } }
/// <summary> /// 接收完成时触发此事件,完成接收消息的初步分类 /// </summary> /// <param name="receiveBuffer">缓冲接收字节</param> /// <param name="receiveSize">接收字节数</param> void ReceiveCompletedEvent(byte[] receiveBuffer, int receiveSize) { if (receiveSize > 0) { Debug.Log("接收到一条信息!" + Encoding.Default.GetString(receiveBuffer, 0, receiveSize)); this._receiveBuffer = receiveBuffer; this._receivedDataSize = receiveSize; this._isReceivedData = true; //如果不是客户端请求的响应信息,则提交翻译 if ((NetCmd)receiveBuffer[0] == NetCmd.ServerCmd) { NetCmdTranslator.ServerCmdTranslator(receiveBuffer, receiveSize); InitializeReceiveBuff(); } } else { Debug.LogError("接收到0字节信息!"); } }
/// <summary> /// 发送请求给服务器,并等待服务器的回应消息,最多等待echoLife毫秒,此方法有阻塞,最多阻塞echoLife毫秒 /// </summary> /// <param name="bytes">发送的内容</param> /// <returns>接收到则返回接收到的字节数组,长度是实际收到的字节数,未接收到则返回null</returns> public byte[] Send(byte[] bytes) { _isSending = true; if (!SingleSend(bytes)) { _isSending = false; return(null); } //发送成功继续执行接收服务器回应信息 int sendTime = DateTime.Now.Second * 1000 + DateTime.Now.Millisecond; int timeDelta; while (true) { //未收到信息,则被阻塞 timeDelta = (DateTime.Now.Second * 1000 + DateTime.Now.Millisecond) - sendTime; timeDelta = timeDelta >= 0 ? timeDelta : (timeDelta + 60000); if (timeDelta > echoLife) { _isSending = false; return(null); } if (_isReceivedData) //接收到信息,不仅仅是响应,还有可能来自于服务端的命令(请求) { if (NetCmdTranslator.CheckProtocal(bytes, _receiveBuffer)) { Debug.Log("接收到" + _receivedDataSize + "个字节"); byte[] newBytes = new byte[_receivedDataSize]; Array.Copy(_receiveBuffer, newBytes, _receivedDataSize); InitializeReceiveBuff(); _isSending = false; return(newBytes); } } Thread.Sleep(1); } }