Esempio n. 1
0
        public async Task <IActionResult> Channel(ChannelAddressModel model)
        {
            var lastReadTime = DateTime.UtcNow;
            var channel      = await _dbContext.Channels.FindAsync(model.Id);

            if (channel == null)
            {
                return(this.Protocol(ErrorType.NotFound, "Can not find channel with id: " + model.Id));
            }
            if (channel.ConnectKey != model.Key)
            {
                return(Json(new AiurProtocol
                {
                    Code = ErrorType.Unauthorized,
                    Message = "Wrong connection key!"
                }));
            }
            await _pusher.Accept(HttpContext);

            int sleepTime = 0;

            while (_pusher.Connected)
            {
                try
                {
                    var nextMessages = _memoryContext
                                       .Messages
                                       .Where(t => t.ChannelId == model.Id)
                                       .Where(t => t.CreateTime > lastReadTime)
                                       .ToList();
                    if (!nextMessages.Any())
                    {
                        if (sleepTime < 1000)
                        {
                            sleepTime += 5;
                        }
                        await Task.Delay(sleepTime);
                    }
                    else
                    {
                        var nextMessage = nextMessages.OrderBy(t => t.CreateTime).First();
                        await _pusher.SendMessage(nextMessage.Content);

                        lastReadTime = nextMessage.CreateTime;
                        sleepTime    = 0;
                    }
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return(null);
        }
Esempio n. 2
0
        public async Task <IActionResult> Channel(ChannelAddressModel model)
        {
            var lastReadTime = DateTime.Now;
            var channel      = await _dbContext.Channels.FindAsync(model.Id);

            if (channel.ConnectKey != model.Key)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.Unauthorized,
                    message = "Wrong connection key!"
                }));
            }
            await _pusher.Accept(HttpContext);

            int sleepTime = 0;

            while (_pusher.Connected)
            {
                try
                {
                    var nextMessages = MqMemoryContext
                                       .Messages
                                       .Where(t => t.ChannelId == model.Id)
                                       .Where(t => t.CreateTime > lastReadTime)
                                       .ToList();
                    if (!nextMessages.Any())
                    {
                        if (sleepTime < 300)
                        {
                            sleepTime += 5;
                        }
                        await Task.Delay(sleepTime);
                    }
                    else
                    {
                        var nextMessage = nextMessages.OrderBy(t => t.CreateTime).First();
                        await _pusher.SendMessage(nextMessage.Content);

                        lastReadTime = nextMessage.CreateTime;
                        sleepTime    = 0;
                    }
                }
                catch (InvalidOperationException)
                {
                }
            }
            return(null);
        }
Esempio n. 3
0
 public async Task<IActionResult> Pushing()
 {
     await pusher.Accept(HttpContext);
     for (int i = 0; i < 36000 && pusher.Connected; i++)
     {
         try
         {
             pusher.SendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff") + $"|{i + 1}").GetAwaiter();
             await Task.Delay(100);
         }
         catch
         {
             break;
         }
     }
     return null;
 }
Esempio n. 4
0
        public async Task <IActionResult> Pushing()
        {
            await _pusher.Accept(HttpContext);

            while (_pusher.Connected)
            {
                try
                {
                    _pusher.SendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff")).GetAwaiter();
                    await Task.Delay(100);
                }
                catch
                {
                    break;
                }
            }
            return(null);
        }
Esempio n. 5
0
        public async Task <IActionResult> Pushing()
        {
            if (!HttpContext.WebSockets.IsWebSocketRequest)
            {
                return(Json(new { }));
            }
            await _pusher.Accept(HttpContext);

            for (int i = 0; i < 36000 && _pusher.Connected; i++)
            {
                try
                {
                    _pusher.SendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff") + $"|{i + 1}").GetAwaiter();
                    await Task.Delay(100);
                }
                catch
                {
                    break;
                }
            }
            return(Json(new { }));
        }