public async Task <IActionResult> Channel(ChannelAddressModel model) { int lastReadId = _counter.GetCurrent; 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.Id > lastReadId) .ToList(); if (!nextMessages.Any()) { if (sleepTime < 1000) { sleepTime += 5; } await Task.Delay(sleepTime); } else { var nextMessage = nextMessages.OrderBy(t => t.Id).FirstOrDefault(); if (nextMessage != null) { await _pusher.SendMessage(nextMessage.Content); lastReadId = nextMessage.Id; sleepTime = 0; } } } catch (InvalidOperationException e) { _logger.LogError(e, e.Message); } } return(Json(new { })); }
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(this.Protocol(ErrorType.Unauthorized, "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); }
public async Task <IActionResult> Channel(ChannelAddressModel model) { int lastReadId = _counter.GetCurrent; var channel = _memoryContext[model.Id]; if (channel == null) { return(this.Protocol(ErrorType.NotFound, "Can not find channel with id: " + model.Id)); } if (channel.ConnectKey != model.Key) { return(this.Protocol(new AiurProtocol { Code = ErrorType.Unauthorized, Message = "Wrong connection key!" })); } await _pusher.Accept(HttpContext); int sleepTime = 0; try { await Task.Factory.StartNew(_pusher.PendingClose); channel.ConnectedUsers++; while (_pusher.Connected && channel.IsAlive()) { channel.LastAccessTime = DateTime.UtcNow; var nextMessages = channel .GetMessagesFrom(lastReadId) .ToList(); if (nextMessages.Any()) { var messageToPush = nextMessages.OrderBy(t => t.Id).FirstOrDefault(); if (messageToPush != null) { await _pusher.SendMessage(messageToPush.Content); lastReadId = messageToPush.Id; sleepTime = 0; } } else { if (sleepTime < 1000) { sleepTime += 5; } await Task.Delay(sleepTime); } } } catch (Exception e) { _logger.LogError(e, e.Message); var accessToken = _appsContainer.AccessToken(); await _eventService.LogExceptionAsync(await accessToken, e, Request.Path); } finally { channel.ConnectedUsers--; await _pusher.Close(); } return(this.Protocol(new AiurProtocol { Code = ErrorType.UnknownError, Message = "You shouldn't be here." })); }
public async Task <IActionResult> Channel(ChannelAddressModel model) { int lastReadId = _counter.GetCurrent; 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; try { _connectedCountService.AddConnectedCount(channel.Id); await Task.Factory.StartNew(_pusher.PendingClose); _lastAccessService.RecordLastConnectTime(channel.Id); while (_pusher.Connected && _channelLiveJudger.IsAlive(channel.Id)) { var nextMessages = _memoryContext .Messages .Where(t => t.ChannelId == model.Id) .Where(t => t.Id > lastReadId) .ToList(); if (!nextMessages.Any()) { if (sleepTime < 1000) { sleepTime += 5; } await Task.Delay(sleepTime); } else { var nextMessage = nextMessages.OrderBy(t => t.Id).FirstOrDefault(); if (nextMessage != null) { await _pusher.SendMessage(nextMessage.Content); lastReadId = nextMessage.Id; sleepTime = 0; } } } _lastAccessService.RecordLastConnectTime(channel.Id); } catch (Exception e) { _logger.LogError(e, e.Message); var accessToken = _appsContainer.AccessToken(); await _eventService.LogAsync(await accessToken, e.Message, e.StackTrace, EventLevel.Exception, Request.Path); } finally { _connectedCountService.ReduceConnectedCount(channel.Id); } return(Json(new { })); }