// Función de recepción de audio... private void HandleIncommingStreamingAudio(ref NetworkEvent evt) { MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData; // Se extrae el mensaje del campo de datos que llega desde la red en la variable data.... byte[] data = evt.GetDataAsByteArray(); // Los datos se pasan a float para que puedan ser reproducidos por el componente Audio Source de Unity... float[] audioData = ToFloatArray(data); // Se almacena un número de muestras de audio... for (int i = 0; i < audioData.Length; i++) { read.Add(audioData[i]); } // Cuando el número de muestras almacedanas es igual/mayor a la frecuencia, se reproducen... if (data != null && read.Count >= FREQUENCY_RATE) { AudioSource audioSource = GetComponent <AudioSource>(); speakers.SetData(read.ToArray(), 0); audioSource.clip = speakers; audioSource.Play(); read.Clear(); } //return the buffer so the network can reuse it buffer.Dispose(); }
public static string GetDataAsString(this NetworkEvent netEvent) { if (netEvent.MessageData == null) { return(netEvent.Type.ToString()); } var bytes = netEvent.GetDataAsByteArray(); return(Encoding.UTF8.GetString(bytes, 0, bytes.Length)); }
private void Server_OnReceivedMessage(NetworkEvent message) { byte[] messageBytes = message.GetDataAsByteArray(); lock (server.peers) { foreach (KeyValuePair <string, ConnectionId> peer in server.peers) { if (peer.Value != message.ConnectionId) { server.SendMessage(peer.Value, messageBytes, false); } } } }
private void HandleIncommingStreamingVideoAndText(ref NetworkEvent evt) { MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData; // Se extrae el mensaje del campo de datos que llega desde la red en la variable data.... byte[] data = evt.GetDataAsByteArray(); // Si el mensaje que llega es mayor a 250, son datos de vídeo... if (data != null && data.Length > 250) { timerToggleTexture = 3.0f; getTexture.LoadImage(data); getTexture.Apply(); CamaraWeb.GetComponent <Image>().enabled = true; CamaraWeb.GetComponent <Image>().material.mainTexture = getTexture; } // Si el mensaje que llega es menor a 250, son datos de texto... else if (data != null && data.Length <= 250) { // Se extrae el mensaje de los bytes... string msg = Encoding.UTF8.GetString(buffer.Buffer, 0, buffer.ContentLength); // Se pinta el mensaje en el chat... if (mIsServer) { SendString(msg); Append(msg); } else { Append(msg); } } //return the buffer so the network can reuse it buffer.Dispose(); }
void OnMessageReceived(NetworkEvent netEvent, bool reliable) { var bytes = netEvent.GetDataAsByteArray(); var packet = Packet.Deserialize(bytes); // If packet is null, it is a "raw" byte array message. // Forward it to everyone if (packet == null) { OnGetBytes.TryInvoke(netEvent.ConnectionId, bytes, reliable); foreach (var r in ConnectionIds) { // Forward to everyone except the original sender and the server if (r == CId || r == netEvent.ConnectionId) { continue; } Send(Packet.From(CId).To(r).With(ReservedTags.PacketForwarding, packet.Serialize()), true); } return; } string reservedTag = packet.Tag.StartsWith("reserved") ? packet.Tag : string.Empty; // If is not a reserved message if (reservedTag == string.Empty) { OnGetPacket.TryInvoke(netEvent.ConnectionId, packet, reliable); if (NodeState != State.Server) { return; } // The server tries to broadcast the packet to everyone else listed as recipients foreach (var r in packet.Recipients) { // Forward to everyone except the original sender and the server if (r == CId.id || r == netEvent.ConnectionId.id) { continue; } Send(Packet.From(CId).To(r).With(ReservedTags.PacketForwarding, packet.Serialize()), true); } return; } // handle reserved messages switch (reservedTag) { case ReservedTags.ServerStopped: OnServerStopped.TryInvoke(); break; case ReservedTags.ClientJoined: ConnectionIds.Add(netEvent.ConnectionId); OnJoin.TryInvoke(netEvent.ConnectionId); break; case ReservedTags.ClientLeft: ConnectionIds.Remove(netEvent.ConnectionId); OnLeave.TryInvoke(netEvent.ConnectionId); break; case ReservedTags.PacketForwarding: OnGetPacket.TryInvoke(netEvent.ConnectionId, packet, reliable); break; } }