// Update is called once per frame
 void Update()
 {
     if (!initialised)
     {
         try {
             GameObject camera       = GameObject.FindGameObjectsWithTag("MainCamera") [0];
             TestSocket socketScript = camera.GetComponent(typeof(TestSocket)) as TestSocket;
             socket = socketScript.getSocket();
             socket.registerObserver(outlet);
             initialised = true;
             Debug.Log("Successfully Initialised Presentation Board");
         } catch (NullReferenceException ex) {
         }
     }
     else
     {
         if (!outlet.IsOutletEmpty())
         {
             Message m = outlet.GetNextMessage();
             if (m.Signal == "CHNG")
             {
                 ByteBuffer buffer = new ByteBuffer(m.Payload);
                 currentSlideNumber = buffer.ReadInt();                     //The only way the current slide number changes is if a CHNG message is received
                 FixSlideNumber();
                 meetingDisplay.SetCurrentSlideNumber(currentSlideNumber);
                 StartCoroutine(ChangeImage());                     //The only way the image on the board will change is if a CHNG message is received
             }
         }
     }
 }
    // Update is called once per frame
    void Update()
    {
        if (Time.time >= playNextSampleAt)
        {
            float[] receivedAudioSamples = GetStoredSamples();
            if (receivedAudioSamples != null)
            {
                PlayAudioSamples(receivedAudioSamples);
            }
        }

        if (!outlet.IsOutletEmpty())            //Check for any new messages and extract the audio samples from them
        {
            Message message = outlet.GetNextMessage();
            if (message != null)
            {
                byte[]       payload     = message.Payload;     //Get the payload containing the samples
                byte[]       tempFloat   = new byte[4];         //Create a temp byte array to store 1 sample
                List <float> tempSamples = new List <float> (); //Create a list to store each sample that's produced from the payload

                for (int i = 0; i < payload.Length; i++)        //Take every 4 bytes, convert it to a float and add it to the temp list
                {
                    if (i > 0 && i % 4 == 0)
                    {
                        tempSamples.Add(new ByteBuffer(tempFloat).ReadFloat());
                    }
                    tempFloat[i % 4] = payload[i];
                }

                AddSamplesToList(tempSamples.ToArray());
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!initialised)
        {
            try {
                GameObject camera       = GameObject.FindGameObjectsWithTag("MainCamera") [0];
                TestSocket socketScript = camera.GetComponent(typeof(TestSocket)) as TestSocket;
                socket = socketScript.getSocket();
                socket.registerObserver(outlet);

                initialised = true;
                Debug.Log("Successfully Initialised Player Manager");
                Message ma = new Message("GAP", new byte[0]);
                socket.sendMessage(ma);
            } catch (NullReferenceException ex) {
            }
        }
        else
        {
            if (!outlet.IsOutletEmpty())
            {
                Message m = outlet.GetNextMessage();

                if (m.Signal.Equals(ServerSignals.LEFT.ToString()))
                {
                    int  userID = BitConverter.ToInt32(m.Payload, 0);
                    User user   = GetUser(userID);

                    if (user != null)
                    {
                        Debug.Log("User " + user.userID + ":" + user.firstName + " has left");
                        RemoveUser(userID);
                    }
                }
                else if (m.Signal.Equals(ServerSignals.UDM.ToString()))
                {
                    string userInfo = System.Text.ASCIIEncoding.ASCII.GetString(m.Payload);
                    User   user     = JsonUtility.FromJson <User> (userInfo);

                    if (user.presenting)
                    {
                        meetingDisplay.SetPresenterName(user.firstName);
                    }

                    if (user.userID == ReadFile.getUserID())
                    {
                        Configuration.Config.isClientPresenter = user.presenting;
                        button1.SetActive(user.presenting);
                        button2.SetActive(user.presenting);
                        presenterScript.enabled   = user.presenting;
                        participantScript.enabled = !user.presenting;
                    }

                    AddUser(user);
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!initialised)
        {
            try {
                GameObject camera       = GameObject.FindGameObjectsWithTag("MainCamera") [0];
                TestSocket socketScript = camera.GetComponent(typeof(TestSocket)) as TestSocket;
                socket = socketScript.getSocket();
                socket.registerObserver(presenterAudioOutlet);
                initialised = true;
                //Debug.Log("Successfully Initialised Meeting Participant Script");
            } catch (NullReferenceException ex) {
            }
        }
        else
        {
            if (!presenterAudioOutlet.IsOutletEmpty())                 //Whenever you receive a message
            {
                Message next = presenterAudioOutlet.GetNextMessage();  //Get that message

                if (next.Signal.Equals(ServerSignals.AUDI.ToString())) //Add the payload to the buffer of data for the next audio sample
                {
                    payloadBuffer.Add(next.Payload);
                }
                else if (next.Signal.Equals(ServerSignals.EAUD.ToString()))                       //Take all of the payloads obtained until this point and append them to make 1 byte[] then decompress the array and convert it to an audio clip
                {
                    ByteBuffer combinedPayload = new ByteBuffer();
                    //Debug.Log ("Received full audio clip. It took " + payloadBuffer.Count + " messages to receive the audio");

                    foreach (byte[] currentPayload in payloadBuffer)
                    {
                        combinedPayload.WriteBytes(currentPayload);
                    }
                    payloadBuffer.Clear();


                    int originalDataLength = BitConverter.ToInt32(next.Payload, 0);

//					DecompressAndStore (new CompressedData (combinedPayload.ToBytes (), originalDataLength));
                    Thread decompressThread = new Thread(DecompressAndStore);
                    Debug.Log("Decompressing " + combinedPayload.ToBytes().Length + " bytes");
                    decompressThread.Start(new CompressedData(combinedPayload.ToBytes(), originalDataLength));
                }
            }

            if (canOutput)                         //If the initial 15 seconds has passed
            {
                if (Time.time >= playNextSampleAt) // If it's time to play the next sample i.e the time since the last sample has started + it's length has elapsed
                {
                    if (!IsClipQueueEmpty())       //If there's a full clip to play
                    {
                        //Debug.Log("Playing audio");
                        AudioClip nextClip = GetNextClipInQueue();
                        if (nextClip != null)
                        {
                            //Debug.Log("Playing audio clip that is " + nextClip.length + " seconds long");
                            lastSampleStartedAt = Time.time;
                            playNextSampleAt    = Time.time + nextClip.length;
                            AudioSource.PlayClipAtPoint(nextClip, new Vector3(0, 0, 0));
                        }
                    }
                }
            }
        }
    }