Exemplo n.º 1
0
    private void HandleIncommingMessage(ref NetworkEvent evt)
    {
        MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData;

        string msg = Encoding.UTF8.GetString(buffer.Buffer, 0, buffer.ContentLength);

        string s_dataUrlPrefix = "data:image/png;base64,";

        //if server -> forward the message to everyone else including the sender
        if (mIsServer)
        {
            //we use the server side connection id to identify the client
            string idAndMessage = msg;
            SendString(idAndMessage);
            Append(idAndMessage);
        }
        else
        {
            //check for pic

            if (msg.StartsWith(s_dataUrlPrefix))
            {
                ClickPic.ReceiveImage(msg);
            }
            else
            {
                //client received a message from the server -> simply print
                //if message received
                Append(msg);
            }
        }

        //return the buffer so the network can reuse it
        buffer.Dispose();
    }
Exemplo n.º 2
0
    // 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();
    }
Exemplo n.º 3
0
    private void HandleIncommingMessage(ref NetworkEvent evt)
    {
        MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData;

        if (buffer.Buffer[0] == 0)
        {
        }

        string msg = Encoding.UTF8.GetString(buffer.Buffer, 0, buffer.ContentLength);

        //if server -> forward the message to everyone else including the sender
        if (mIsServer)
        {
            //we use the server side connection id to identify the client
            string idAndMessage = evt.ConnectionId + ":" + msg;
            SendString(idAndMessage);
            Append(idAndMessage);
        }
        else
        {
            //client received a message from the server -> simply print
            Append(msg);
        }

        //return the buffer so the network can reuse it
        buffer.Dispose();
    }
Exemplo n.º 4
0
    private string ToString(MessageDataBuffer buff)
    {
        if (buff == null)
        {
            return("no data");
        }
        if (buff.Buffer == null)
        {
            return("content empty");
        }
        string content = Encoding.ASCII.GetString(buff.Buffer, 0, buff.ContentLength);

        return(content);
    }
Exemplo n.º 5
0
    private void HandleIncommingMessage(ref NetworkEvent evt)
    {
        MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData;
        string            msg    = Encoding.UTF8.GetString(buffer.Buffer, 0, buffer.ContentLength);

        try {
            //Debug.Log ("Processing message: " + msg);
            Message message = JsonConvert.DeserializeObject <Message> (msg, jssettings);

            if (message == null)
            {
                Debug.Log("Json was null!");
            }
            message.process();

            if (state.isServer && message.mt != MessageType.ServerOnly)
            {
                foreach (ConnectionId id in mConnections)
                {
                    if (id != evt.ConnectionId)
                    {
                        SendMessage(message, id.id, false);
                    }
                }
            }
        } catch (System.NullReferenceException e) {
            Debug.Log("Exception thrown: " + e.ToString());
            if (msg != null)
            {
                Debug.Log(msg);
            }
            else
            {
                Debug.Log("A message was null :(");
            }
        }
        //return the buffer so the network can reuse it
        buffer.Dispose();
    }
Exemplo n.º 6
0
    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();
    }
Exemplo n.º 7
0
    private void FixedUpdate()
    {
        if (mServer != null)
        {
            mServer.Update();

            NetworkEvent evt;
            while (mServer.Dequeue(out evt))
            {
                Debug.Log("Server received event " + evt);
                if (evt.Type == NetEventType.ServerInitialized)
                {
                    Debug.Log("Test " + mState + " successful!");
                    //server is started -> connect client
                    mState = State.ConnectClient;
                    Debug.Log("Start test " + mState);
                    mClient.Connect(evt.Info);
                }
                else if (evt.Type == NetEventType.ReliableMessageReceived)
                {
                    MessageDataBuffer buff = evt.MessageData;
                    Debug.Log("server reliable message received:" + ToString(buff));
                    mServer.SendData(evt.ConnectionId, buff.Buffer, 0, buff.ContentLength, true);
                    buff.Dispose();
                }
                else if (evt.Type == NetEventType.UnreliableMessageReceived)
                {
                    MessageDataBuffer buff = (MessageDataBuffer)evt.MessageData;
                    Debug.Log("server unreliable message received:" + ToString(buff));
                    mServer.SendData(evt.ConnectionId, buff.Buffer, 0, buff.ContentLength, false);
                    buff.Dispose();
                }
                else if (evt.Type == NetEventType.Disconnected)
                {
                    Debug.Log("Shutdown server");
                    mServer.Shutdown();

                    StartCoroutine(CoroutineCleanup());
                }
            }
        }


        if (mClient != null)
        {
            mClient.Update();
            NetworkEvent evt;
            while (mClient.Dequeue(out evt))
            {
                Debug.Log("Client received event " + evt);

                if (evt.Type == NetEventType.NewConnection)
                {
                    //send out test message

                    Debug.Log("Test " + mState + " successful!");
                    mState = State.ReliableMessage;
                    Debug.Log("Start test " + mState);
                    byte[] data2 = Encoding.ASCII.GetBytes(mReliableTestMessage);
                    mClient.SendData(evt.ConnectionId, data2, 0, data2.Length, true);
                }
                else if (evt.Type == NetEventType.ReliableMessageReceived)
                {
                    MessageDataBuffer buff = evt.MessageData;

                    string recMessage = ToString(buff);
                    Debug.Log("client reliable message received:" + recMessage);
                    if (mReliableTestMessage == recMessage)
                    {
                        Debug.Log("Reliable channel works");
                    }
                    else
                    {
                        Debug.LogError("Expected " + mReliableTestMessage + " not " + recMessage);
                    }
                    buff.Dispose();

                    Debug.Log("Test " + mState + " successful!");
                    mState = State.UnreliableMessage;
                    Debug.Log("Start test " + mState);
                    byte[] data1 = Encoding.ASCII.GetBytes(mUnreliableTestMessage);
                    mClient.SendData(evt.ConnectionId, data1, 0, data1.Length, false);
                }
                else if (evt.Type == NetEventType.UnreliableMessageReceived)
                {
                    MessageDataBuffer buff       = evt.MessageData;
                    string            recMessage = ToString(buff);
                    Debug.Log("client unreliable message received:" + recMessage);
                    if (mUnreliableTestMessage == recMessage)
                    {
                        Debug.Log("Unreliable channel works");
                    }
                    else
                    {
                        Debug.LogError("Expected " + mUnreliableTestMessage + " not " + recMessage);
                    }
                    buff.Dispose();

                    Debug.Log("Test " + mState + " successful!");
                    mState = State.TestSuccessful;
                    Debug.Log("All tests done!");

                    Debug.Log("Shutdown client");
                    mClient.Shutdown();
                }
            }
        }
    }
Exemplo n.º 8
0
 private string ToString(MessageDataBuffer buff)
 {
     if (buff == null)
         return "no data";
     if (buff.Buffer == null)
         return "content empty";
     string content = Encoding.ASCII.GetString(buff.Buffer, 0, buff.ContentLength);
     return content;
 }
Exemplo n.º 9
0
    private void HandleIncommingData(ref NetworkEvent evt)
    {
        MessageDataBuffer buffer = (MessageDataBuffer)evt.MessageData;

        //Disconnection request from MeshSender
        if (buffer.Buffer[0] == 0xFF && !mIsServer)
        {
            if (serverClosed != null)
            {
                serverClosed();
            }
            buffer.Dispose();
            Reset();
            return;
        }

        //preparing is complete as break signal
        if (buffer.Buffer[0] == 0x30 && mIsServer)
        {
            for (int i = 0; i < mConnections.Count; i++)
            {
                if (evt.ConnectionId.Equals(mConnections[i].Key))
                {
                    ConnectionStatus status = new ConnectionStatus();
                    status.isReadyForStream = true;
                    if (buffer.Buffer[1] == 0x01)
                    {
                        status.isCompressStream = true;
                    }
                    else
                    {
                        status.isCompressStream = false;
                    }
                    mConnections[i] =
                        new KeyValuePair <ConnectionId, ConnectionStatus>(mConnections[i].Key, status);
                    break;
                }
            }
            return;
        }

        //stream is complete
        if (buffer.Buffer[0] == 0x31 && mIsServer)
        {
            for (int i = 0; i < mConnections.Count; i++)
            {
                if (evt.ConnectionId.Equals(mConnections[i].Key))
                {
                    ConnectionStatus status = mConnections[i].Value;
                    status.sendQueue -= 1;
                    if (status.isTemporalStop && status.sendQueue < 1)
                    {
                        status.isTemporalStop = false;
                        status.sendQueue      = 0;
                        //Debug.Log("WebRTC: restart stream queuing in " + mConnections[i].Key.ToString());
                    }
                    mConnections[i] =
                        new KeyValuePair <ConnectionId, ConnectionStatus>(mConnections[i].Key, status);
                    break;
                }
            }
            return;
        }

        byte[] dataBuffer = new byte[buffer.ContentLength];
        Buffer.BlockCopy(buffer.Buffer, 0, dataBuffer, 0, buffer.ContentLength);

        if (buffer.Buffer[0] == 0x01 && !mIsServer)
        {
            serverID = evt.ConnectionId;
            if (firstResponce != null)
            {
                firstResponce(dataBuffer);
                buffer.Dispose();
                return;
            }
        }

        if (dataReceived != null)
        {
            dataReceived(dataBuffer, evt.ConnectionId);
        }

        //return the buffer so the network can reuse it
        buffer.Dispose();
    }