Beispiel #1
0
        public void Init()
        {
            /* 向管理器註冊本體 */
            if (WebSocket != null)
            {
                while (!webSocketManage.RegisterService(WebSocketId, this))
                {
                    WebSocketId = Guid.NewGuid().ToString();
                }
            }

            /* 初始化 Message Queue */
            channel = mqConnection.CreateModel();

            channel.QueueDeclare(
                queue: "task_queue",
                durable: true,
                exclusive: false,
                autoDelete: false,
                arguments: null
                );

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                /* 如果有在服務用戶,拒絕此次要求 */
                if (work)
                {
                    channel.BasicReject(ea.DeliveryTag, true);
                    return;
                }

                String body = Encoding.UTF8.GetString(ea.Body);

                try
                {
                    data = JsonConvert.DeserializeObject <Dictionary <String, String> >(body);

                    WebSocketClientId = data["websocketId"];
                    clientQuestion    = data["question"];
                    clientName        = data["name"];
                    clientMail        = data["mail"];
                    clientPhone       = data["phone"];

                    if (!webSocketManage.SetWebSocketServiceId(WebSocketClientId, WebSocketId))
                    {
                        return;
                    }

                    webSocketManage.SendToService(WebSocketId, body);

                    webSocketManage.SendToClient(WebSocketClientId, JsonConvert.SerializeObject(new { Start = true }));

                    AddDialogue(body);

                    work = true;
                }
                catch (Exception)
                {
                }
                finally
                {
                    channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                }
            };

            channel.BasicConsume(queue: "task_queue", autoAck: false, consumer: consumer);
        }
Beispiel #2
0
 public void Reset()
 {
     webSocketManage.SendToClient(WebSocketId, JsonConvert.SerializeObject(new { End = true }));
     WebSocketServiceId = "";
 }