コード例 #1
0
        public async Task <IActionResult> ValidateChannel(ChannelAddressModel model)
        {
            var channel = await _dbContext.Channels.FindAsync(model.Id);

            if (channel == null)
            {
                return(Json(new AiurProtocol
                {
                    Code = ErrorType.NotFound,
                    Message = "Can not find your channel!"
                }));
            }
            if (!_channelLiveJudger.IsAlive(channel.Id))
            {
                return(Json(new AiurProtocol
                {
                    Code = ErrorType.Pending,
                    Message = "Your channel is out dated and about to be deleted!"
                }));
            }
            if (channel.ConnectKey != model.Key)
            {
                return(Json(new AiurProtocol
                {
                    Code = ErrorType.Unauthorized,
                    Message = "Wrong connection key!"
                }));
            }
            else
            {
                return(Json(new AiurValue <string>(channel.AppId)
                {
                    Code = ErrorType.Success,
                    Message = $"Current Info. Belongs to app with id: '{channel.AppId}'."
                }));
            }
        }
コード例 #2
0
        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 { }));
        }