Пример #1
0
        public void OnConnected(Android.OS.Bundle connectionHint)
        {
            resolving = false;
            SignedOut = false;
            signingIn = false;

            OnSignedIn?.Invoke(this, EventArgs.Empty);
        }
    private void OnAuthStateChanged(object sender, System.EventArgs e)
    {
        if (auth.CurrentUser != user)
        {
            bool isSignedIn = user != auth.CurrentUser && auth.CurrentUser != null;

            if (!isSignedIn && user != null)
            {
                Debug.Log("Signed out: " + user.UserId);

                OnSignedOut?.Invoke();
            }

            user = auth.CurrentUser;

            if (isSignedIn)
            {
                Debug.Log("Signed in " + user.UserId);

                OnSignedIn?.Invoke();
            }
        }
    }
Пример #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 <FurioosRoutedMessage <SignalingMessage> >(content);

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

                if (!string.IsNullOrEmpty(msg.type))
                {
                    if (msg.type == "signIn")
                    {
                        if (msg.status == "SUCCESS")
                        {
                            Debug.Log("Signaling: Slot signed in.");
                            this.WSSend("{\"type\":\"furioos\",\"task\":\"enableStreaming\",\"streamTypes\":\"WebRTC\",\"controlType\":\"RenderStreaming\"}");

                            OnSignedIn?.Invoke(this);
                        }
                        else
                        {
                            Debug.LogError("Signaling: Sign-in error : " + msg.message);
                        }
                    }
                    else if (msg.type == "reconnect")
                    {
                        if (msg.status == "SUCCESS")
                        {
                            Debug.Log("Signaling: Slot reconnected.");
                        }
                        else
                        {
                            Debug.LogError("Signaling: Reconnect error : " + msg.message);
                        }
                    }

                    if (msg.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 (!string.IsNullOrEmpty(msg.candidate))
                {
                    if (!string.IsNullOrEmpty(routedMessage.from))
                    {
                        CandidateData candidate = new CandidateData();
                        candidate.connectionId  = routedMessage.from;
                        candidate.candidate     = msg.candidate;
                        candidate.sdpMLineIndex = msg.sdpMLineIndex;
                        candidate.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);
            }
        }
Пример #4
0
        /// <summary>
        /// Connects to the server.
        /// </summary>
        /// <param name="uri">the server to connect to</param>
        /// <param name="client_name">Client name.</param>
        public async void Connect(string uri, string client_name)
        {
            try
            {
                if (_state != State.NOT_CONNECTED)
                {
                    OnServerConnectionFailure?.Invoke(new Exception("_state is invalid at start"));
                    return;
                }

                _connectUri = new Uri(uri);
                _clientName = client_name;

                if (_httpClient != null)
                {
                    _httpClient.Dispose();
                }
                _httpClient = new HttpClient();

                _httpClient.DefaultRequestHeaders.Host = _connectUri.Host;

                if (_authHeader != null)
                {
                    _httpClient.DefaultRequestHeaders.Authorization = _authHeader;
                }

                _httpClient.BaseAddress = _connectUri;

                // set this to 2 minutes, which is the azure max
                // the value here is really fairly arbitrary, since
                // our long polling endpoint will just reconnect if it times out
                _httpClient.Timeout = TimeSpan.FromMinutes(2);

                var res = await this._httpClient.GetAsync($"/sign_in?peer_name={client_name}");

                if (res.StatusCode != HttpStatusCode.OK)
                {
                    OnServerConnectionFailure?.Invoke(new Exception("invalid response status " + res.StatusCode));
                    return;
                }

                // set the id, from the request
                _myId = int.Parse(res.Headers.Pragma.ToString());

                var body = await res.Content.ReadAsStringAsync();

                // we pass _myId as pragmaId, since they're the same value in this case
                if (!ParseServerResponse(body, _myId))
                {
                    OnServerConnectionFailure?.Invoke(new Exception("unable to parse response"));
                    return;
                }

                _state = State.CONNECTED;

                OnSignedIn?.Invoke();

                // Start the long polling loop without await
                StartHangingGetReadLoop();

                // start our heartbeat timer
                if (_heartbeatTimer == null)
                {
                    _heartbeatTimer = new Timer(
                        this.HeartbeatLoopAsync, null, 0, _heartbeatTickMs);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("[Error] Signaling: Failed to connect to server: " + ex.Message);
                OnServerConnectionFailure?.Invoke(ex);
            }
        }