public async Task SendMessageAsync(string userId, JokerMessage message)
        {
            var client = _hubContext.Clients.User(userId);

            if (client == null)
            {
                System.Diagnostics.Debug.WriteLine("Could not find a client with Id: " + userId);
                return;
            }

            await client.SendAsync("receiveMessage", message);
        }
        public async Task ReceiveMessageAsync(JokerMessage message)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var targetUserIds = await _subsManager.GetSubscribedIdsAsync(message.ConversationId);

            if (!targetUserIds.Any())
            {
                return;
            }

            targetUserIds = targetUserIds.Distinct().ToArray();
            var messageJson = JsonConvert.SerializeObject(message);

            using (var httpClient = new HttpClient()) {
                foreach (var target in targetUserIds)
                {
                    var registration = await _registrationsManager.GetRegistrationForUserId(target);

                    if (registration == null)
                    {
                        continue;
                    }

                    var hostname = registration.EndpointHostName;
                    if (hostname == "::1" || hostname == "127.0.0.1") // IPv6 hostname
                    {
                        hostname = "localhost";
                    }

                    var targetUrl = $"http://{hostname}:{registration.EndpointPort}/Messages/SendMessageToUser?userId={target}";
                    using var request = new HttpRequestMessage(HttpMethod.Post, targetUrl)
                          {
                              Content = new StringContent(messageJson, Encoding.UTF8, "application/json")
                          };

                    try {
                        var response = await httpClient.SendAsync(request);

                        response.EnsureSuccessStatusCode();
                    } catch (Exception) {
                        System.Diagnostics.Debug.WriteLine("Could not send a message to a user");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private async void StartDebugMessageStream()
        {
            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));

                var debugMessage = new JokerMessage {
                    Content      = "DEBUG",
                    DateSent     = DateTime.UtcNow,
                    Id           = Guid.NewGuid(),
                    SenderName   = "Debug Sender",
                    SenderUserId = Guid.NewGuid()
                };

                OnMessageArrived(debugMessage);
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> SendMessageToUser([FromQuery] string userId, [FromBody] JokerMessage message)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return(NotFound());
            }

            await _messageForwarder.SendMessageAsync(userId, message);

            return(Ok());
        }
Ejemplo n.º 5
0
 private void OnMessageArrived(JokerMessage incomingMessage)
 {
     MessagesToDisplay.Add(incomingMessage);
     StateHasChanged();
 }
Ejemplo n.º 6
0
 public async Task SendMessage([FromBody] JokerMessage message)
 {
     await _receptionOrchestrator.ReceiveMessageAsync(message);
 }