Exemplo n.º 1
0
        /// <summary>
        /// Handles incoming signaling connection request from the registered listening service.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ConnectionRequestEventArgs{DemoCredentials, DemoAdapterInformation}"/> instance containing the event data.</param>
        private async void OnServiceConnectionRequest(object sender, ConnectionRequestEventArgs <DemoCredentials, DemoAdapterInformation> e)
        {
            Logger.LogInformation($"Connection request from {e.RemoteAdapterInformation.Name}.");

            if (IsInSession)
            {
                Logger.LogInformation($"Already in session. Connection request declined.");
                e.Decline();
                return;
            }

            try
            {
                Logger.LogInformation("Starting SignalR session for signaling...");

                SelectedClient = ConnectedClients.FirstOrDefault(x => x.ServiceId == e.RemoteAdapterInformation.Name);

                IsFree = false;

                _signalingTransporter = ResonanceTransporter.Builder
                                        .Create()
                                        .WithAdapter(e.Accept())
                                        .WithJsonTranscoding()
                                        .Build();

                _signalingTransporter.ConnectionLost += OnSignalingConnectionLost;

                await _signalingTransporter.ConnectAsync();

                Logger.LogInformation("SignalR session started. Waiting for WebRTC offer...");

                Logger.LogInformation("Connecting WebRTC transporter...");
                _webRtcTransporter = ResonanceTransporter.Builder
                                     .Create()
                                     .WithWebRTCAdapter()
                                     .WithSignalingTransporter(_signalingTransporter)
                                     .WithRole(WebRTCAdapterRole.Accept)
                                     .WithDefaultIceServers()
                                     .WithJsonTranscoding()
                                     .Build();

                _webRtcTransporter.ConnectionLost += OnWebRTCConnectionLost;
                _webRtcTransporter.RegisterRequestHandler <EchoTextRequest, EchoTextResponse>(OnEchoTextMessageReceived);

                await _webRtcTransporter.ConnectAsync();

                IsInSession = true;
                IsFree      = true;

                Logger.LogInformation("WebRTC transporter connected!");
            }
            catch (Exception ex)
            {
                IsFree = true;
                Logger.LogError(ex, "");
            }
        }
Exemplo n.º 2
0
        private async void _service_ConnectionRequest(object sender, ConnectionRequestEventArgs <DemoCredentials, DemoAdapterInformation> e)
        {
            if (!await ShowQuestionMessage($"Client wants to connect to this service. Do you accept?"))
            {
                e.Decline();
                return;
            }

            ResonanceSignalRClient newClient = new ResonanceSignalRClient();

            newClient.RemoteAdapterInformation = e.RemoteAdapterInformation;

            newClient.StateChanged += OnClientStateChanged;

            var adapter = e.Accept();

            adapter.Credentials.Name = $"{ServiceId} {adapter}";

            newClient.CreateBuilder()
            .WithAdapter(adapter)
            .WithJsonTranscoding()
            .Build();

            newClient.RegisterRequestHandler <EchoTextRequest, EchoTextResponse>((request) =>
            {
                Logger.LogInformation($"{newClient.RemoteAdapterInformation.Name} says: {request.Message}");
                return(new EchoTextResponse()
                {
                    Message = "OK"
                });
            });

            await newClient.ConnectAsync();

            InvokeUI(() =>
            {
                Clients.Add(newClient);
            });

            Logger.LogInformation($"{newClient.RemoteAdapterInformation.Name} is now connected.");
        }