void OnReceive(IAsyncResult ar) { try { IPEndPoint ipEndpoint = null; byte[] data = connection.EndReceive(ar, ref ipEndpoint); UDPChat.AddClient(ipEndpoint); string message = System.Text.Encoding.UTF8.GetString(data); //Message Processes according to messageType ProtocolHandler.Handle(message, ipEndpoint); if (UDPChat.instance.isServer && message.Substring(0, 2) != ProtocolLabels.clientLeft && message.Substring(0, 2) != ProtocolLabels.joinRequest && message.Substring(0, 2) != ProtocolLabels.playerMove && message.Substring(0, 2) != ProtocolLabels.playerRot) { UDPChat.BroadcastChatMessage(message, ipEndpoint); } UDPChat.instance.connectionFailed = false; } catch (SocketException e) { Debug.Log(e); UDPChat.instance.connectionFailed = true; } connection.BeginReceive(OnReceive, null); }
void OnReceive(IAsyncResult ar) { try { IPEndPoint ipEndpoint = null; byte[] data = connection.EndReceive(ar, ref ipEndpoint); UDPChat.AddClient(ipEndpoint); string message = System.Text.Encoding.UTF8.GetString(data); UDPChat.messageToDisplay += message + Environment.NewLine; if (UDPChat.instance.isServer) { UDPChat.BroadcastChatMessage(message); } } catch (SocketException e) { // This happens when a client disconnects, as we fail to send to that port. } connection.BeginReceive(OnReceive, null); }