Ejemplo n.º 1
0
        /// <summary>
        /// Callback which is fired when client connects to hub.
        /// </summary>
        /// <returns></returns>
        public override Task OnConnected()
        {
#if !UNAUTHORIZED_DEBUG
            try
            {
                // Find httpContext of request.
                var httpContext = Context.Request.GetHttpContext();

                // Find ip address comes from request.
                var ipAddress = _loginDomain.FindRequestIpAddress(httpContext);

                // Analyze ip address.
                var ips = _loginDomain.AnalyzeIpAddress(ipAddress);

                // Find terminal from ip address.
                var terminal = _loginDomain.FindTerminalFromIpAddressAsync(ips);

                Log.Debug(
                    string.Format(
                        "(C3) IP : {0} - Terminal : {1} - Connection Index: {2} has connected. Await for checking...",
                        ipAddress, terminal, Context.ConnectionId));

                // Update realtime connection record into database.
                var realtimeConnections = _unitOfWork.RealtimeConnectionRepository.GetAll();
                realtimeConnections = realtimeConnections.Where(x => x.Index.Equals(Context.ConnectionId));

                // Find the connection.
                var realtimeConnection = realtimeConnections.FirstOrDefault();
                if (realtimeConnection == null)
                {
                    realtimeConnection = new RealtimeConnection();
                    Log.Debug("(C3) Initiate new connection record");
                }
                else
                {
                    Log.Debug("(C3) Update existing records");
                }

                realtimeConnection.Index      = Context.ConnectionId;
                realtimeConnection.Joined     = DateTime.Now;
                realtimeConnection.TerminalNo = terminal.F06_TerminalNo;

                // Add or update.
                _unitOfWork.RealtimeConnectionRepository.AddOrUpdate(realtimeConnection);

                // Commit changes asynchronously.
                _unitOfWork.Commit();
            }
            catch (Exception exception)
            {
                Log.Error(exception.Message, exception);
            }
#endif

            return(base.OnConnected());
        }
Ejemplo n.º 2
0
        protected override void OnInitialized()
        {
            if (RealtimeConnection is null || Configuration is null)
            {
                Updates = new RingBuffer <string>(capacity: 0);

                return;
            }

            Updates = new RingBuffer <string>(Configuration.GetValue(key: "Updates-BufferSize", defaultValue: 10));

            PrimeUpdates();

            RealtimeConnection.Closed += async _ =>
            {
                ErrorMessage = Empty;
                Connected    = false;

                if (Receive)
                {
                    await RealtimeConnection.StartAsync();
                }
            };

            RealtimeConnection.Reconnected += _ =>
            {
                ErrorMessage = Empty;
                Connected    = true;

                return(CompletedTask);
            };

            RealtimeConnection.Reconnecting += _ =>
            {
                ErrorMessage = Empty;
                Connected    = false;

                return(CompletedTask);
            };

            RealtimeConnection.On <string>
            (
                methodName: "statusUpdate"
                , status =>
            {
                Updates.Write(status);

                StateHasChanged();
            }
            );
        }
Ejemplo n.º 3
0
        // ReSharper disable once UnusedMember.Global
        protected async Task UpdatesClicked()
        {
            if (RealtimeConnection is null)
            {
                ErrorMessage = "SignalR Connection was not configured";

                return;
            }

            ErrorMessage = Empty;

            Receive = !Receive;

            if (Receive)
            {
                try
                {
                    Connected = true;

                    await RealtimeConnection.StartAsync();
                }
                catch (Exception exception)
                {
                    ErrorMessage = exception.Message;
                    Connected    = false;
                }
            }
            else
            {
                try
                {
                    await RealtimeConnection.StopAsync();

                    PrimeUpdates();
                }
                catch (Exception exception)
                {
                    ErrorMessage = exception.Message;
                }
            }
        }