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

            if (channel == null)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.NotFound,
                    message = "Can not find your channel!"
                }));
            }
            if (channel.ConnectKey != model.Key)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.Unauthorized,
                    message = "Wrong connection key!"
                }));
            }
            else
            {
                return(Protocal(ErrorType.Success, "Corrent Info."));
            }
        }
Example #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;

            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(Json(new AiurProtocal
                {
                    Code = ErrorType.Unauthorized,
                    Message = "Wrong connection key!"
                }));
            }
            await _pusher.Accept(HttpContext);

            int sleepTime = 0;

            while (_pusher.Connected)
            {
                try
                {
                    var nextMessages = StargateMemory
                                       .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);
        }
Example #4
0
        public async Task <IActionResult> IntegratedTest()
        {
            var token  = AppsContainer.AccessToken();
            var result = await ChannelService.CreateChannelAsync(await token(), "Test Channel");

            await Task.Factory.StartNew(async() =>
            {
                await _debugger.SendDebuggingMessages(await token(), result.ChannelId);
            });

            var model = new ChannelAddressModel
            {
                Id  = result.ChannelId,
                Key = result.ConnectKey
            };

            return(View("Test", model));
        }
Example #5
0
        public async Task <IActionResult> IntegratedTest()
        {
            var token = await _appsContainer.AccessToken();

            var result = await _channelService.CreateChannelAsync(token, "Test Channel");

            _cannonService.FireAsync <DebugMessageSender>(d =>
            {
                return(d.SendDebuggingMessages(token, result.ChannelId));
            });
            var model = new ChannelAddressModel
            {
                Id  = result.ChannelId,
                Key = result.ConnectKey
            };

            return(View("Test", model));
        }
Example #6
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}'."
                }));
            }
        }
        public IActionResult ValidateChannel(ChannelAddressModel model)
        {
            var channel = _stargateMemory[model.Id];

            if (channel == null)
            {
                return(Ok(new AiurProtocol
                {
                    Code = ErrorType.NotFound,
                    Message = "Can not find your channel!"
                }));
            }
            if (channel.IsDead())
            {
                return(Ok(new AiurProtocol
                {
                    Code = ErrorType.Pending,
                    Message = "Your channel is out dated and about to be deleted!"
                }));
            }
            if (channel.ConnectKey != model.Key)
            {
                return(Ok(new AiurProtocol
                {
                    Code = ErrorType.Unauthorized,
                    Message = "Wrong connection key!"
                }));
            }
            else
            {
                return(Ok(new AiurValue <string>(channel.AppId)
                {
                    Code = ErrorType.Success,
                    Message = $"Current Info. Belongs to app with id: '{channel.AppId}'."
                }));
            }
        }
Example #8
0
        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."
            }));
        }
Example #9
0
 public IActionResult ListenTo(ChannelAddressModel model)
 {
     return(View("Test", model));
 }
Example #10
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 { }));
        }