Esempio n. 1
0
        public override void Stop()
        {
            base.Stop();
            // 处理队列中剩余的消息
            Package package;

            while (_sendingQueue.TryTake(out package))
            {
                if (package != null)
                {
                    OnMessageSending?.Invoke(this, package);
                    OnInnerSending(package);
                    OnMessageSent?.Invoke(this, package);
                }
            }
        }
Esempio n. 2
0
        private void PorcessMessageQueue()
        {
            while (true)
            {
                if (_shutdownEvent.Wait(0)) // 仅检查标志,立即返回
                {
                    // 关闭事件触发,退出loop
                    return;
                }

                var package = _sendingQueue.Take();
                if (package != null)
                {
                    OnMessageSending?.Invoke(this, package);
                    OnInnerSending(package);
                    OnMessageSent?.Invoke(this, package);
                }
            }
        }
Esempio n. 3
0
        public override async void SendMessageAsync(RouteMessage message)
        {
            var routeMsg = (CoolQRouteMessage)message;

            OnMessageSending?.Invoke(routeMsg);
            if (routeMsg.Canceled && !routeMsg.IsForced)
            {
                return;
            }

            if (routeMsg.DelayTime > TimeSpan.FromSeconds(0))
            {
                await Task.Delay(routeMsg.DelayTime).ConfigureAwait(false);
            }

            var msg = (routeMsg.EnableAt && routeMsg.MessageType != MessageType.Private
                          ? new At(routeMsg.UserId) + " "
                          : "") + ((CoolQMessage)routeMsg.Message).Compose();
            string info = routeMsg.Identity.ToString();
            string status;

            switch (routeMsg.MessageType)
            {
            case MessageType.Group:
                status = CoolQHttpApiClient.SendGroupMessageAsync(routeMsg.GroupId, msg).Status;
                break;

            case MessageType.Discuss:
                status = CoolQHttpApiClient.SendDiscussMessageAsync(routeMsg.DiscussId, msg).Status;
                break;

            case MessageType.Private:
                status = CoolQHttpApiClient.SendPrivateMessageAsync(routeMsg.UserId, msg).Status;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Logger.Message(string.Format("({0}) 我: {{status: {1}}}\r\n  {2}", info, status, CoolQCode.DecodeToString(msg)));
        }