public void RoomNameChanged(string value) { client.Close(); client = new UdpClient(localEP); if (remoteEndPoint != null) { MpTextMessage message = new MpTextMessage("room_changeName", gameController.multiplayController.currentRoomId + "," + value); try { client.Connect(remoteEndPoint); } catch { return; } byte[] sendBuffer = message.GetBytes(); client.BeginSend(sendBuffer, sendBuffer.Length, new AsyncCallback(RoomNameChangedCallback), null); //add a timer connectTimer = gameController.thisgameObj.AddComponent <Timer>(); connectTimer.liveTime = 5; connectTimer.Timeout += CreateRoomFailed; connectTimer.Start(); } }
public void LeaveRoom() { client.Close(); client = new UdpClient(localEP); isCreateRoomFailed = false; if (remoteEndPoint != null) { MpTextMessage message = new MpTextMessage("leave_room", currentRoomId.ToString() + "," + gameController.player_guid); try { client.Connect(remoteEndPoint); } catch { return; } byte[] sendBuffer = message.GetBytes(); client.BeginSend(sendBuffer, sendBuffer.Length, new AsyncCallback(LeaveRoomSendCallback), null); //add a timer connectTimer = gameController.thisgameObj.AddComponent <Timer>(); connectTimer.liveTime = 5; connectTimer.Timeout += CreateRoomFailed; connectTimer.Start(); } }
public void GetRoomList() { client.Close(); client = new UdpClient(localEP); if (remoteEndPoint != null) { MpTextMessage message = new MpTextMessage("room_list"); try { client.Connect(remoteEndPoint); } catch { GetRoomListFailed(); return; } byte[] sendBuffer = message.GetBytes(); client.BeginSend(sendBuffer, sendBuffer.Length, new AsyncCallback(GetRoomListSendCallabck), null); //add a timer connectTimer = gameController.thisgameObj.AddComponent <Timer>(); connectTimer.liveTime = 5; connectTimer.Timeout += GetRoomListFailed; connectTimer.Start(); } }
private void ConnectRecvCallback(IAsyncResult iar) { if (iar.IsCompleted) { byte[] receiveBytes = new byte[0]; try { receiveBytes = client.EndReceive(iar, ref remoteSenderEP); } catch { ConnectFailed(); return; } if (receiveBytes.Length > 0) { string s = Encoding.UTF8.GetString(receiveBytes); MpTextMessage message = JsonUtility.FromJson <MpTextMessage>(s); if (message.content == "success") { ConnectSuccess(); } else { ConnectFailed(); return; } } else { ConnectFailed(); return; } } }
public void TryConnect() { client = new UdpClient(); if (remoteEndPoint != null) { //连接的同时提交玩家的名称和guid MpTextMessage message = new MpTextMessage("connect", gameController.player_name + "," + gameController.player_guid); if (gameController.panel_multiplay_inscene != null) { txt_connect = gameController.panel_multiplay_inscene.transform.Find("txt_connect").gameObject; if (txt_connect != null) { txt_connect.GetComponent <Text>().text = "连接中......"; } } try { client.Connect(remoteEndPoint); localEP = (IPEndPoint)client.Client.LocalEndPoint; } catch { ConnectFailed(); return; } byte[] sendBuffer = message.GetBytes(); client.BeginSend(sendBuffer, sendBuffer.Length, new AsyncCallback(ConnectSendCallback), null); //add a timer connectTimer = gameController.thisgameObj.AddComponent <Timer>(); connectTimer.liveTime = 5; connectTimer.Timeout += ConnectFailed; connectTimer.Start(); } }
private void SendResponseWithoutRecv(string type, string content, IPEndPoint endPoint) { MpTextMessage response = new MpTextMessage(type, content); byte[] sendBuffer = response.GetBytes(); //发送回复 try { server.Connect(endPoint); } catch { Log("无法建立到客户端: " + endPoint.ToString() + " 的连接。"); } server.BeginSend(sendBuffer, sendBuffer.Length, sendCallBackWithoutRecv, new UdpSendState(server, response.ToString())); }