Esempio n. 1
0
 /// <summary>
 /// Initializes new instance of data.
 /// </summary>
 /// <param name="client">The client who has connected.</param>
 /// <param name="httpRequest">The httpRequest of the request which initiated this event.</param>
 /// <param name="lastEventId">The identifier of last event which client has received.</param>
 public ServerSentEventsClientConnectedArgs(IServerSentEventsClient client, HttpRequest httpRequest, string lastEventId = null)
     : this()
 {
     Client      = client;
     HttpRequest = httpRequest;
     LastEventId = lastEventId;
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes new instance of data.
 /// </summary>
 /// <param name="request">The request which has been made in order to establish the connection.</param>
 /// <param name="client">The client who has connected.</param>
 /// <param name="lastEventId">The identifier of last event which client has received.</param>
 public ServerSentEventsClientConnectedArgs(HttpRequest request, IServerSentEventsClient client, string lastEventId)
     : this()
 {
     Request     = request;
     Client      = client;
     LastEventId = lastEventId;
 }
Esempio n. 3
0
 /// <summary>
 /// Initializes new instance of data.
 /// </summary>
 /// <param name="request">The request which has been made in order to establish the connection.</param>
 /// <param name="client">The client who has connected.</param>
 public ServerSentEventsClientConnectedArgs(HttpRequest request, IServerSentEventsClient client)
     : this()
 {
     Request     = request;
     Client      = client;
     LastEventId = null;
 }
Esempio n. 4
0
 /// <summary>
 /// When overriden in delivered class allows for recovery when client has reestablished the connection.
 /// </summary>
 /// <param name="client">The client who has reestablished the connection.</param>
 /// <param name="lastEventId">The identifier of last event which client has received.</param>
 /// <returns>The task object representing the asynchronous operation.</returns>
 public virtual Task OnReconnectAsync(IServerSentEventsClient client, string lastEventId)
 {
     if (_logger != null && _logger.IsEnabled(LogLevel.Trace))
     {
         _logger.LogTrace(Constants.c_eventServiceTraceId, Constants.m_initiateSSEReconnect);
     }
     return(System.Threading.TaskHelper.GetCompletedTask());
 }
Esempio n. 5
0
 internal void RemoveClient(ServerSentEventsClient client)
 {
     // If the client has a userId, check if the same client is stored in _userClients for the same userId, remove if match found
     if (client.UserId.HasValue)
     {
         IServerSentEventsClient userClient = GetUserClient(client.UserId.Value);
         if (userClient != null && userClient.Id.Equals(client.Id))
         {
             _userClients.TryRemove(client.UserId.Value, out userClient);
         }
     }
     client.IsConnected = false;
     _clients.TryRemove(client.Id, out client);
 }
Esempio n. 6
0
        public async Task Start_WhenEventStreamClientStartedSuccessfully_ReturnsSelf()
        {
            var stream = new MemoryStream();

            m_mockedEventStreamClient.Setup(c => c.StartAsync()).ReturnsAsync(stream);
            m_mockedEventStreamProcessor.Setup(p => p.ProcessAsync(stream, It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);
            m_mockedEventStreamProcessor.SetupSet(p => p.OnMessage = It.IsAny <Action <ServerSentEventsMessage> >());

            IServerSentEventsClient result = await m_sut.Start();

            Assert.That(result, Is.EqualTo(m_sut));
            m_mockRepository.VerifyAll();
        }
Esempio n. 7
0
        public void SetUp()
        {
            m_mockRepository             = new MockRepository(MockBehavior.Strict);
            m_mockedEventStreamClient    = m_mockRepository.Create <IEventStreamClient>();
            m_mockedEventStreamProcessor = m_mockRepository.Create <IEventStreamProcessor>();
            m_mockedLoggerFactory        = new Mock <ILoggerFactory>();

            m_mockedLoggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(Mock.Of <ILogger>());

            m_sut = new ServerSentEventsClient(
                m_mockedEventStreamClient.Object,
                m_mockedEventStreamProcessor.Object,
                m_mockedLoggerFactory.Object
                );
        }
Esempio n. 8
0
        public override Task OnConnectAsync(HttpRequest request, IServerSentEventsClient client)
        {
            IList <Claim> claimCollection = new List <Claim>
            {
                new Claim(ClaimTypes.Name, "Andras")
                , new Claim(ClaimTypes.Country, "Sweden")
                , new Claim(ClaimTypes.Gender, "M")
                , new Claim(ClaimTypes.Surname, "Nemes")
                , new Claim(ClaimTypes.Email, "*****@*****.**")
                , new Claim(ClaimTypes.Role, "IT")
            };

            ClaimsIdentity claimsIdentity = new ClaimsIdentity(claimCollection);

            client.User.AddIdentity(claimsIdentity);
            return(base.OnConnectAsync(request, client));
        }
Esempio n. 9
0
        private async Task pushDockerBuildStream(IServerSentEventsClient client, Stream buildOutput)
        {
            using (var reader = new StreamReader(buildOutput))
            {
                while (!reader.EndOfStream)
                {
                    var result = await reader.ReadLineAsync();

                    if (result.Contains("Success"))
                    {
                    }
                    await client.SendEventAsync(new ServerSentEvent()
                    {
                        Type = "BuildOutput",
                        Data = new string[] { result }
                    });
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Adds a new IServerSentEventsClient to the list of connected clients
        /// </summary>
        /// <param name="client">IServerSentEventsClient implementation representing the client.</param>
        /// <returns>Guid that can be used to track the client.</returns>
        Guid IServerSentEventsService.AddClient(IServerSentEventsClient client)
        {
            Guid clientId = Guid.NewGuid();

            if (_clients.TryAdd(clientId, client))
            {
                if (_clients.Count > 0 && UseHeartbeat && (_heartbeat == null || _heartbeat.IsCanceled || _heartbeat.IsCompleted))
                {
                    this._hearbeatCancellation = new CancellationTokenSource();
                    this._hearbeatCancellation.Token.Register(this.OnHeartbeatCancellation);
                    this._heartbeat = Heartbeat(this._hearbeatCancellation.Token);
                }
            }
            if (_logger != null && _logger.IsEnabled(LogLevel.Trace))
            {
                _logger.LogTrace(Constants.c_eventServiceTraceId, Constants.m_registerSSEClient, clientId, _clients.Count);
            }
            return(clientId);
        }
        async Task <IServerSentEventsClient> IServerSentEventsClient.Start()
        {
            IServerSentEventsClient @this = this;

            m_eventStreamProcessor.OnMessage = message =>
            {
                @this.OnMessage?.Invoke(message);
                if (m_eventListeners.TryGetValue(message.Event, out var handlers))
                {
                    foreach (var handler in handlers)
                    {
                        handler.Invoke(message);
                    }
                }
            };

            using (m_logger.BeginScope("Stratring SSE client")) {
                m_logger.LogInformation("Configuring HttpClient");
                @this.Configure?.Invoke(m_eventStreamClient.HttpClient);

                m_logger.LogInformation("Connecting...");

                Stream stream;
                try {
                    stream = await m_eventStreamClient.StartAsync().ConfigureAwait(false);
                }
                catch (Exception e) {
                    m_logger.LogError(0, e, "Failed to connect");
                    throw;
                }

                m_cts = new CancellationTokenSource();
#pragma warning disable 4014
                // Fire & forget
                m_eventStreamProcessor.ProcessAsync(stream, m_cts.Token);
#pragma warning restore 4014
                m_logger.LogInformation("Processing SSE stream has started");
            }

            return(this);
        }
        /// <summary>
        /// Adds a client to the specified group.
        /// </summary>
        /// <param name="groupName">The group name.</param>
        /// /// <param name="client">The client to add to a group.</param>
        /// <returns>The task object representing the result of asynchronous operation</returns>
        public async Task <ServerSentEventsAddToGroupResult> AddToGroupAsync(string groupName, IServerSentEventsClient client)
        {
            ServerSentEventsAddToGroupResult result = ServerSentEventsAddToGroupResult.AddedToExistingGroup;

            if (!_groups.ContainsKey(groupName))
            {
                await CreateGroupAsync(groupName);

                result = ServerSentEventsAddToGroupResult.AddedToNewGroup;
            }

            _groups[groupName].TryAdd(client.Id, client);

            return(result);
        }
Esempio n. 13
0
        /// <summary>
        /// Method which is called when client is disconnecting. The base implementation raises the <see cref="ClientDisconnected"/> event.
        /// </summary>
        /// <param name="client">The client who is disconnecting.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public virtual Task OnDisconnectAsync(IServerSentEventsClient client)
        {
            ClientDisconnected?.Invoke(this, new ServerSentEventsClientDisconnectedArgs(client));

            return(TaskHelper.GetCompletedTask());
        }
Esempio n. 14
0
        /// <summary>
        /// Method which is called when client is reestablishing the connection. The base implementation raises the <see cref="ClientConnected"/> event.
        /// </summary>
        /// <param name="client">The client who is reestablishing the connection.</param>
        /// <param name="httpRequest">The httpRequest of the request which initiated this event.</param>
        /// <param name="lastEventId">The identifier of last event which client has received.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public virtual Task OnReconnectAsync(IServerSentEventsClient client, HttpRequest httpRequest, string lastEventId)
        {
            ClientConnected?.Invoke(this, new ServerSentEventsClientConnectedArgs(client, httpRequest, lastEventId));

            return(TaskHelper.GetCompletedTask());
        }
Esempio n. 15
0
 public override Task OnDisconnectAsync(HttpRequest request, IServerSentEventsClient client)
 {
     return(base.OnDisconnectAsync(request, client));
 }
 /// <summary>
 /// Initializes new instance of data.
 /// </summary>
 /// <param name="request">The original request which has been made in order to establish the connection.</param>
 /// <param name="client">The client who has disconnected.</param>
 public ServerSentEventsClientDisconnectedArgs(HttpRequest request, IServerSentEventsClient client)
     : this()
 {
     Request = request;
     Client  = client;
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes new instance of data.
 /// </summary>
 /// <param name="client">The client who has disconnected.</param>
 public ServerSentEventsClientDisconnectedArgs(IServerSentEventsClient client)
     : this()
 {
     Client = client;
 }
 /// <summary>
 /// When overriden in delivered class allows for recovery when client has reestablished the connection.
 /// </summary>
 /// <param name="client">The client who has reestablished the connection.</param>
 /// <param name="lastEventId">The identifier of last event which client has received.</param>
 /// <returns>The task object representing the asynchronous operation.</returns>
 public virtual Task OnReconnectAsync(IServerSentEventsClient client, string lastEventId)
 {
     return(TaskHelper.GetCompletedTask());
 }
        /// <summary>
        /// Method which is called when client is reestablishing the connection. The base implementation raises the <see cref="ClientConnected"/> event.
        /// </summary>
        /// <param name="request">The request which has been made in order to establish the connection.</param>
        /// <param name="client">The client who is reestablishing the connection.</param>
        /// <param name="lastEventId">The identifier of last event which client has received.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public virtual Task OnReconnectAsync(HttpRequest request, IServerSentEventsClient client, string lastEventId)
        {
            ClientConnected?.Invoke(this, new ServerSentEventsClientConnectedArgs(request, client, lastEventId));

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Method which is called when client is disconnecting. The base implementation raises the <see cref="ClientDisconnected"/> event.
        /// </summary>
        /// <param name="request">The original request which has been made in order to establish the connection.</param>
        /// <param name="client">The client who is disconnecting.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public virtual Task OnDisconnectAsync(HttpRequest request, IServerSentEventsClient client)
        {
            ClientDisconnected?.Invoke(this, new ServerSentEventsClientDisconnectedArgs(request, client));

            return(Task.CompletedTask);
        }
        void IServerSentEventsClient.Stop()
        {
            IServerSentEventsClient @this = this;

            @this.Dispose();
        }