public EventWaitHandle SendMsg(string connectionId, byte[] sendBytes, Func <string, byte[], bool> check, int timeOut)
        {
            MsgDto newMsg = new MsgDto
            {
                SendBytes  = sendBytes,
                SendTime   = DateTime.Now,
                WaitHandle = new AutoResetEvent(false),
                Check      = check,
                TimeOut    = timeOut
            };

            Task.Run(() =>
            {
                if (!_msgList.ContainsKey(connectionId))
                {
                    _msgList.TryAdd(connectionId, new SynchronizedCollection <MsgDto>());
                }
                var sendList = _msgList[connectionId];
                sendList.Add(newMsg);
                Task.Run(() =>
                {
                    Thread.Sleep(timeOut);
                    sendList.Remove(newMsg);
                });
            });

            return(newMsg.WaitHandle);
        }
        private void DeleteMsg(string connectionId, MsgDto msg)
        {
            if (!_msgList.ContainsKey(connectionId))
            {
                _msgList.TryAdd(connectionId, new SynchronizedCollection <MsgDto>());
            }
            var sendList = _msgList[connectionId];

            sendList.Remove(msg);
        }
예제 #3
0
 public void Send(MsgDto msg)
 {
     using (var connection = factory.CreateConnection())
     {
         using (var channel = connection.CreateModel())
         {
             channel.QueueDeclare("chat_queue", false, false, false, null); //创建一个名称为hello的消息队列
             var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
             channel.BasicPublish("", "chat_queue", null, body);            //开始传递
         }
     }
 }