コード例 #1
0
        public override Task OnDisconnected(bool stopCalled)
        {
            var disconnectedUser = ConnectedClients.FirstOrDefault(x => x.Id.Equals(Context.ConnectionId));

            ConnectedClients.Remove(disconnectedUser);

            using (var _contextDB = new db_chatjobsityEntities())
            {
                var _objDb_disco = new Disconnected()
                {
                    UserID    = disconnectedUser.UserName,
                    Fe_Salida = System.DateTime.UtcNow.AddHours(-5)
                };
                _contextDB.Disconnected.Add(_objDb_disco);
                _contextDB.SaveChanges();

                var _discoUser = (from x in _contextDB.Disconnected
                                  orderby x.Fe_Salida descending
                                  select x).AsEnumerable().Take(50);
                var _discoUserX = (from x in _discoUser
                                   select new { UserName = x.Fe_Salida.ToString("yyyy-MM-dd HH:mm:ss") + " - " + x.UserID.ToString() }).ToList();

                Clients.All.updateUsers(ConnectedClients.Count(), ConnectedClients.Select(x => x.UserName), _discoUserX.Select(x => x.UserName));
            }
            return(base.OnDisconnected(stopCalled));
        }
コード例 #2
0
ファイル: MainWindowVM.cs プロジェクト: royben/Resonance
        /// <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, "");
            }
        }
コード例 #3
0
        protected override void OnClientConnect(IAsyncResult result)
        {
            if (Token.IsCancellationRequested)
            {
                return;
            }

            CanAcceptConnections.Set();
            try
            {
                ISocketState state;

                lock (ConnectedClients)
                {
                    var id = !ConnectedClients.Any() ? 1 : ConnectedClients.Keys.Max() + 1;

                    state = new SocketState(((Socket)result.AsyncState).EndAccept(result), id);


                    //If the server shouldn't accept the IP do nothing.
                    if (!IsConnectionAllowed(state))
                    {
                        return;
                    }

                    var client = ConnectedClients.FirstOrDefault(x => x.Value == state);

                    if (client.Value == state)
                    {
                        id = client.Key;
                        ConnectedClients.Remove(id);
                        ConnectedClients.Add(id, state);
                    }
                    else
                    {
                        ConnectedClients.Add(id, state);
                    }

                    ClientConnectedInvoke(id, state);
                }

                StartReceiving(state);
            }
            catch (ObjectDisposedException ode)
            {
                InvokeErrorThrown(ode);
            }
            catch (SocketException se)
            {
                this.InvokeErrorThrown(se);
            }
        }
コード例 #4
0
        private IClient AddNotExistClientToCollection(Socket handler)
        {
            //handler.SendFile("", null, null, T)
            var     address     = handler.RemoteEndPoint.ToString();
            IClient client      = new Client(handler, _dataConverter);
            var     foundClient = ConnectedClients.FirstOrDefault(p => p.Address == address);

            if (foundClient != null)
            {
                client = foundClient;
            }
            else
            {
                ConnectedClients.Add(client);
                client.SentMessage += OnSentMessage;
            }

            return(client);
        }