Exemple #1
0
        private bool HTTPConnect(string connectionId)
        {
            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create($"{m_url}/signaling/connection");

            request.Method      = "PUT";
            request.ContentType = "application/json";
            request.Headers.Add("Session-Id", m_sessionId);
            request.KeepAlive = false;

            using (Stream dataStream = request.GetRequestStream())
            {
                byte[] bytes = new System.Text.UTF8Encoding().GetBytes($"{{\"connectionId\":\"{connectionId}\"}}");
                dataStream.Write(bytes, 0, bytes.Length);
                dataStream.Close();
            }

            HttpWebResponse         response = HTTPGetResponse(request);
            CreateConnectionResData data     = HTTPParseJsonResponse <CreateConnectionResData>(response);

            if (data == null)
            {
                return(false);
            }

            Debug.Log("Signaling: HTTP create connection, connectionId : " + connectionId);
            m_mainThreadContext.Post(d => OnCreateConnection?.Invoke(this, data.connectionId, data.polite), null);
            return(true);
        }
        private bool HTTPConnect()
        {
            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create($"{m_url}/signaling/connection");

            request.Method      = "PUT";
            request.ContentType = "application/json";
            request.Headers.Add("Session-Id", m_sessionId);
            request.KeepAlive = false;

            HttpWebResponse         response = HTTPGetResponse(request);
            CreateConnectionResData data     = HTTPParseJsonResponse <CreateConnectionResData>(response);

            if (data == null)
            {
                return(false);
            }

            m_lastTimeGetOfferRequest = DateTimeExtension.ParseHttpDate(response.Headers[HttpResponseHeader.Date])
                                        .ToJsMilliseconds();

            OnCreateConnection?.Invoke(this, data.connectionId);

            return(true);
        }
Exemple #3
0
        private void WSProcessMessage(object sender, MessageEventArgs e)
        {
            var content = Encoding.UTF8.GetString(e.RawData);

            Debug.Log($"Signaling: Receiving message: {content}");

            try
            {
                var routedMessage = JsonUtility.FromJson <RoutedMessage <SignalingMessage> >(content);

                SignalingMessage msg;
                if (!string.IsNullOrEmpty(routedMessage.type))
                {
                    msg = routedMessage.data;
                }
                else
                {
                    msg = JsonUtility.FromJson <SignalingMessage>(content);
                }

                if (!string.IsNullOrEmpty(routedMessage.type))
                {
                    if (routedMessage.type == "connect")
                    {
                        string connectionId = JsonUtility.FromJson <SignalingMessage>(content).connectionId;
                        OnCreateConnection?.Invoke(this, connectionId);
                    }
                    else if (routedMessage.type == "offer")
                    {
                        if (!string.IsNullOrEmpty(routedMessage.from))
                        {
                            DescData offer = new DescData();
                            offer.connectionId = routedMessage.from;
                            offer.sdp          = msg.sdp;

                            OnOffer?.Invoke(this, offer);
                        }
                        else
                        {
                            Debug.LogError("Signaling: Received message from unknown peer");
                        }
                    }
                    else if (routedMessage.type == "answer")
                    {
                        if (!string.IsNullOrEmpty(routedMessage.from))
                        {
                            DescData answer = new DescData
                            {
                                connectionId = routedMessage.from,
                                sdp          = msg.sdp
                            };
                            OnAnswer?.Invoke(this, answer);
                        }
                        else
                        {
                            Debug.LogError("Signaling: Received message from unknown peer");
                        }
                    }
                    else if (routedMessage.type == "candidate")
                    {
                        if (!string.IsNullOrEmpty(routedMessage.from))
                        {
                            CandidateData candidate = new CandidateData
                            {
                                connectionId  = routedMessage.@from,
                                candidate     = msg.candidate,
                                sdpMLineIndex = msg.sdpMLineIndex,
                                sdpMid        = msg.sdpMid
                            };
                            OnIceCandidate?.Invoke(this, candidate);
                        }
                        else
                        {
                            Debug.LogError("Signaling: Received message from unknown peer");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("Signaling: Failed to parse message: " + ex);
            }
        }
    //private void ProcessMessage(byte[] data)
    private void ProcessMessage(string content)
    {
        //var content = Encoding.UTF8.GetString(data);
        Debug.Log($"Signaling: Receiving message: {content}");

        try
        {
            var routedMessage = JsonUtility.FromJson <RoutedMessage <SignalingMessage> >(content);

            SignalingMessage msg;
            if (!string.IsNullOrEmpty(routedMessage.type))
            {
                msg = routedMessage.data;
            }
            else
            {
                msg = JsonUtility.FromJson <SignalingMessage>(content);
            }

            if (!string.IsNullOrEmpty(routedMessage.type))
            {
                if (routedMessage.type == "connect")
                {
                    msg = JsonUtility.FromJson <SignalingMessage>(content);
                    m_mainThreadContext.Post(d => OnCreateConnection?.Invoke(this, msg.connectionId, msg.peerExists),
                                             null);
                }
                else if (routedMessage.type == "disconnect")
                {
                    msg = JsonUtility.FromJson <SignalingMessage>(content);
                    m_mainThreadContext.Post(d => OnDestroyConnection?.Invoke(this, msg.connectionId), null);
                }
                else if (routedMessage.type == "offer")
                {
                    DescData offer = new DescData();
                    offer.connectionId = routedMessage.from;
                    offer.sdp          = msg.sdp;
                    m_mainThreadContext.Post(d => OnOffer?.Invoke(this, offer), null);
                }
                else if (routedMessage.type == "answer")
                {
                    DescData answer = new DescData
                    {
                        connectionId = routedMessage.from,
                        sdp          = msg.sdp
                    };
                    m_mainThreadContext.Post(d => OnAnswer?.Invoke(this, answer), null);
                }
                else if (routedMessage.type == "candidate")
                {
                    CandidateData candidate = new CandidateData
                    {
                        connectionId  = routedMessage.@from,
                        candidate     = msg.candidate,
                        sdpMLineIndex = msg.sdpMLineIndex,
                        sdpMid        = msg.sdpMid
                    };
                    m_mainThreadContext.Post(d => OnIceCandidate?.Invoke(this, candidate), null);
                }
                else if (routedMessage.type == "error")
                {
                    msg = JsonUtility.FromJson <SignalingMessage>(content);
                    Debug.LogError(msg.message);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("Signaling: Failed to parse message: " + ex);
        }
    }