public void ConnectAsLobby() { // get messages StartCoroutine(m_mssServerSignalling.UpdateMessages()); //update lobby StartCoroutine(m_mssServerSignalling.UpdateGameLobby(m_mssServerSignalling.m_glbGameLobby.Id, 0, 0)); // check if message is connection offer while (m_mssServerSignalling.m_messagesRecieved.Count > 0) { // get message Tuple <int, string> tupMessage = m_mssServerSignalling.m_messagesRecieved.Dequeue(); //create connection request TestWebRtcConnection twcConnection = new TestWebRtcConnection(); Debug.Log("Recieved offer " + tupMessage.Item2); //process offer and start building reply StartCoroutine(twcConnection.ProcessOffer(tupMessage.Item2)); // mark as still needing to send reply back m_iConnectionsInProgress.Add(tupMessage.Item1); //store connection m_twcConnections[tupMessage.Item1] = twcConnection; } //check if any connections have a reply ready for (int i = m_iConnectionsInProgress.Count - 1; i > -1; i--) { int iTargetPlayerID = m_iConnectionsInProgress[i]; TestWebRtcConnection twcConnection = m_twcConnections[iTargetPlayerID]; //check if connection has finished making reply and is awaiting connection if (twcConnection.State == WebRTCWrapper.State.WaitingToConnect) { string strReply = twcConnection.GetReply(); //send reply to other end of connection StartCoroutine(m_mssServerSignalling.SendMessage( m_mssServerSignalling.m_plpPlayerProfile.Id, iTargetPlayerID, strReply)); Debug.Log($"Sending reply to {iTargetPlayerID} from {m_mssServerSignalling.m_plpPlayerProfile.Id} with value {strReply}"); //remove connection from list of connections awaiting action m_iConnectionsInProgress.RemoveAt(i); } } }
public void ConnectAsNewPlayer() { //check if conenction attempt is in progress if (m_twcConnections.Count == 0) { //create new connection TestWebRtcConnection twcConnection = new TestWebRtcConnection(); //start making offer StartCoroutine(twcConnection.BuildOffer()); //add to list of connections in progress m_iConnectionsInProgress.Add(m_mssServerSignalling.m_plpPlayerProfile.Id); //add to list of connections m_twcConnections.Add(m_mssServerSignalling.m_plpPlayerProfile.Id, twcConnection); } for (int i = m_iConnectionsInProgress.Count - 1; i > -1; i--) { // id of connection int iConnectionId = m_iConnectionsInProgress[i]; TestWebRtcConnection twcConnection = m_twcConnections[iConnectionId]; //check if connection request has finished if (twcConnection.State == WebRTCWrapper.State.WaitingForReply) { // get connection request string strConnectionOffer = twcConnection.GetOffer(); Debug.Log("Sending offer " + strConnectionOffer); //send connection offer StartCoroutine( m_mssServerSignalling.SendMessage( iConnectionId, m_mssServerSignalling.m_glbGameLobby.OwnerId, strConnectionOffer) ); //remove connection from list awaiting locat action m_iConnectionsInProgress.RemoveAt(i); } } // get messages StartCoroutine(m_mssServerSignalling.UpdateMessages()); // loop through all the messages while (m_mssServerSignalling.m_messagesRecieved.Count > 0) { Tuple <int, string> tupMessage = m_mssServerSignalling.m_messagesRecieved.Dequeue(); //check if message from server if (tupMessage.Item1 == m_mssServerSignalling.m_glbGameLobby.OwnerId) { //set it as reply m_twcConnections[m_mssServerSignalling.m_plpPlayerProfile.Id].SetReply(tupMessage.Item2); Debug.Log("Recieved Rpley " + tupMessage.Item2); } } }