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()); }
public void SendMsg(string routeKey, CatMessage message, string queueName = "Cat.IM.Chat") { if (string.IsNullOrWhiteSpace(routeKey)) { _logger.LogInformation($"用户[{message.Chat.Info.Receiver}]不在线!"); return; } _channel.QueueDeclare(queueName, true, false, false, null); _channel.QueueBind(queueName, ExchangeName, routeKey); var basicProperties = _channel.CreateBasicProperties(); basicProperties.DeliveryMode = 2; var address = new PublicationAddress(ExchangeType.Topic, ExchangeName, routeKey); byte[] buffer = null; using (var memoryStream = new MemoryStream()) { using (var output = new Google.Protobuf.CodedOutputStream(memoryStream)) { message.WriteTo(output); } buffer = memoryStream.ToArray(); } _channel.BasicPublish(address, basicProperties, buffer); }
public void Ping(CatMessage message, IChannelHandlerContext context) { context.UpdateReaderTime(DateTime.Now); message = new CatMessage { Type = MessageType.Ping }; context.WriteAndFlushAsync(message); }
public void Executed(CatMessage message, IChannelHandlerContext context) { var type = message.Type; var method = GetType().GetMethod(type.ToString()); if (method == null) { return; } var paramList = new List <object> { message.GetType().GetProperty(type.ToString())?.GetValue(message), context }; method.Invoke(this, paramList.Take(method.GetParameters().Length).ToArray()); }
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()); }
public void Receiver(string routeKey, Action <CatMessage> action = null, string queueName = "Cat.IM.Chat") { _channel.QueueDeclare(queueName, true, false, false, null); _channel.QueueBind(queueName, ExchangeName, routeKey); EventingBasicConsumer consumer = new EventingBasicConsumer(_channel); consumer.Received += (ch, ea) => { using (Google.Protobuf.CodedInputStream input = new Google.Protobuf.CodedInputStream(ea.Body)) { var message = new CatMessage(); message.MergeFrom(input); action?.Invoke(message); } _channel.BasicAck(ea.DeliveryTag, false); }; _channel.BasicConsume(queueName, false, consumer); }
static async Task RunClientAsync() { var group = new MultithreadEventLoopGroup(); try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline .AddLast(new IdleStateHandler(0, 8, 0)) .AddLast(new ProtobufVarint32FrameDecoder()) .AddLast(new ProtobufDecoder(CatMessage.Parser)) .AddLast(new ProtobufVarint32LengthFieldPrepender()) .AddLast(new ProtobufEncoder()) .AddLast(new ClientHandler()); })); IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8850)); while (true) { Console.WriteLine("1.登录 2.发送消息 Q.退出"); var action = Console.ReadLine(); if (action.ToUpper() == "Q") { break; } if (action == "1") { Console.WriteLine("请输入token:"); var token = Console.ReadLine(); var message = new CatMessage { Type = MessageType.Login, Login = new Login { Token = token } }; await clientChannel.WriteAndFlushAsync(message); continue; } //if (action == "2") //{ // Console.WriteLine("请输入接收者ID:"); // var receiverId = Console.ReadLine(); // Console.WriteLine($"请输入发送给{receiverId}的内容:"); // var body = Console.ReadLine(); // var message = new CatMessage // { // Type = CatMessage.Types.MessageType.Chat, // Chat = new Chat // { // Id = Guid.NewGuid().ToString(), // Body = body, // Receiver = receiverId, // SendOn = DateTime.Now.ToString() // } // }; // await clientChannel.WriteAndFlushAsync(message); // continue; //} } await clientChannel.CloseAsync(); } finally { await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); } }