Ejemplo n.º 1
0
        public async Task BroadcastMessage()
        {
            var random  = new Random();
            var hubName = _hubNames[random.Next(_hubNames.Length)];
            var url     = GetBroadcastUrl(hubName);
            var request = new HttpRequestMessage(HttpMethod.Post, GetUrl(url));

            request.Headers.Authorization =
                new AuthenticationHeaderValue("Bearer", _serviceUtils.GenerateAccessToken(url, _serverName));
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var payloadRequest = new PayloadMessage
            {
                Target    = _target,
                Arguments = new[]
                {
                    _serverName,
                    $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}",
                }
            };

            request.Content = new StringContent(JsonConvert.SerializeObject(payloadRequest), Encoding.UTF8, "application/json");
            var response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();
        }
Ejemplo n.º 2
0
        public ServerHandler(string connectionString, string hubName)
        {
            _serverName   = GenerateServerName();
            _serviceUtils = new ServiceUtils(connectionString);
            _hubName      = hubName;
            _endpoint     = _serviceUtils.Endpoint;

            _defaultPayloadMessage = new PayloadMessage
            {
                Target    = "SendMessage",
                Arguments = new[]
                {
                    _serverName,
                    "Hello from server",
                }
            };
        }
        private HttpRequestMessage BuildRequest(string url)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, GetUrl(url));

            request.Headers.Authorization =
                new AuthenticationHeaderValue("Bearer", _serviceUtils.GenerateAccessToken(url, _serverName));
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var payloadRequest = new PayloadMessage
            {
                Target    = _target,
                Arguments = new[]
                {
                    _serverName,
                    $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}",
                }
            };

            request.Content = new StringContent(JsonConvert.SerializeObject(payloadRequest), Encoding.UTF8, "application/json");

            return(request);
        }