Example #1
0
        public async Task <IActionResult> Add(Guid friendId)
        {
            try
            {
                _friendService.Add(friendId);

                var message = new CatMessage
                {
                    Type = MessageType.AddFriend,
                    Base = new Base
                    {
                        Sender   = _workContext.CurrentUser.Id.ToString(),
                        Receiver = friendId.ToString(),
                        SendOn   = DateTime.Now.ToString(),
                    }
                };

                var router = await _cacheManager.GetStringAsync($"{CacheKeys.ROUTER}{friendId}");

                _rabbitManager.SendMsg(router, message);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }
Example #2
0
        public async Task <IActionResult> SendMsg(SendMessageInput input)
        {
            try
            {
                var message = new CatMessage
                {
                    Type = input.Type,
                    Chat = new IM.Google.Protobuf.Chat
                    {
                        Info = new Base
                        {
                            Id       = input.Id.ToString(),
                            Sender   = input.Sender.ToString(),
                            Receiver = input.Receiver.ToString(),
                            SendOn   = input.SendOn.ToString(),
                        },
                        Body = input.Body
                    }
                };

                var routeKey = await _cacheManager.GetStringAsync($"{CacheKeys.ROUTER}{input.Receiver}");

                if (string.IsNullOrWhiteSpace(routeKey))
                {
                    _rabbitManager.SendMsg("Cat.IM.OfflineMessage", message, "Cat.IM.OfflineMessage");
                }
                else
                {
                    _rabbitManager.SendMsg(routeKey, message);
                }


                _chatRecordService.Add(input);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }
Example #3
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            var routerKey = $"{_configuration["Service:IP"]}:{_configuration["Service:Port"]}";

            _rabbitManager.Receiver(routerKey, x =>
            {
                var context = SessionSocketHolder.Get(Guid.Parse(x.Chat.Info.Receiver));

                if (context == null)
                {
                    _logger.LogWarning($"用户[{x.Chat.Info.Receiver}]不在线!");

                    _rabbitManager.SendMsg("Cat.IM.OfflineMessage", x, "Cat.IM.OfflineMessage");

                    return;
                }

                context.WriteAndFlushAsync(x);
            });

            return(Task.CompletedTask);
        }