示例#1
0
        public async Task Start()
        {
            try
            {
                Init();

                using (mqConnection)
                    using (channel)
                    {
                        ArraySegment <Byte> buffer = new ArraySegment <byte>(new Byte[8192]);

                        WebSocketReceiveResult result = await WebSocket.ReceiveAsync(buffer, CancellationToken.None);

                        while (!result.CloseStatus.HasValue)
                        {
                            if (!String.IsNullOrWhiteSpace(WebSocketServiceId))
                            {
                                /* 接收訊息 */
                                using (MemoryStream ms = new MemoryStream())
                                {
                                    ms.Write(buffer.Array, buffer.Offset, result.Count);

                                    while (!result.EndOfMessage)
                                    {
                                        ms.Write(buffer.Array, buffer.Offset, result.Count);

                                        result = await WebSocket.ReceiveAsync(buffer, CancellationToken.None);
                                    }

                                    ms.Seek(0, SeekOrigin.Begin);

                                    using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
                                    {
                                        String message = reader.ReadToEnd();

                                        webSocketManage.SendToService(WebSocketServiceId, message);

                                        AddDialogue(message);
                                    }
                                }
                            }

                            result = await WebSocket.ReceiveAsync(buffer, CancellationToken.None);
                        }

                        await WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
                    }
            }
            catch (Exception)
            {
                mqConnection.Dispose();
                channel.Dispose();
            }

            /* 向管理器反註冊本體 */
            webSocketManage.UnRegisterClient(WebSocketId);
        }
示例#2
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);
        }