Exemplo n.º 1
0
        /// <summary>
        /// Send a message to a specific client
        /// </summary>
        /// <param name="clientPublicToken"></param>
        /// <param name="name"></param>
        /// <param name="contents"></param>
        public void SendMessage(string clientPublicToken, string tip, object content)
        {
            if (string.IsNullOrEmpty(clientPublicToken))
            {
                throw new ArgumentNullException("clientPublicToken");
            }
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            lock (state)
            {
                if (!publicClients.ContainsKey(clientPublicToken))
                {
                    throw CometException.CometClientDoesNotExistException();
                }

                InProcCometClient cometClient = publicClients[clientPublicToken];

                CometMessage message = new CometMessage();

                message.Content   = content;
                message.Tip       = tip;
                message.MessageId = cometClient.NextMessageId;

                cometClient.NextMessageId++;
                cometClient.Messages.Add(message.MessageId, message);
            }
        }
Exemplo n.º 2
0
        private void WriteErrorToResponse(HttpContext context, object objMessage)
        {
            CometMessage errorMessage = new CometMessage();

            errorMessage.Tip       = "aspNetComet.error";
            errorMessage.MessageId = 0;
            errorMessage.Content   = objMessage;

            CometMessage[] messages = new CometMessage[] { errorMessage };

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(messages.GetType());

            serializer.WriteObject(context.Response.OutputStream, messages);

            context.Response.End();
        }
Exemplo n.º 3
0
        /// <summary>
        ///  广播给所有用户
        /// </summary>
        /// <param name="tip">发送提示</param>
        /// <param name="contents">发送内容</param>
        public void SendMessage(string tip, object contents)
        {
            if (contents == null)
            {
                throw new ArgumentNullException("contents");
            }

            lock (state)
            {
                foreach (InProcCometClient cometClient in publicClients.Values)
                {
                    // 将消息放进数组
                    CometMessage message = new CometMessage()
                    {
                        Tip       = tip,
                        Content   = contents,
                        MessageId = cometClient.NextMessageId
                    };

                    cometClient.NextMessageId++;
                    cometClient.Messages.Add(message.MessageId, message);
                }
            }
        }
Exemplo n.º 4
0
        // 线程核心执行函数
        private void QueueCometWaitRequest_WaitCallback()
        {
            while (true)
            {
                CometWaitRequest[] processRequest;

                lock (state)
                {
                    processRequest = WaitRequests.ToArray();
                }
                //if (processRequest.Length == 0)
                //    break;

                if (processRequest.Length == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    for (int i = 0; i < processRequest.Length; i++)
                    {
                        try
                        {
                            CometClient cometClient = StateManager.StateProvider.GetCometClient(processRequest[i].ClientPrivateToken);

                            if (processRequest[i].Active)
                            {
                                Thread.Sleep(100);

                                if (DateTime.Now.Subtract(processRequest[i].DateTimeAdded).TotalSeconds >= cometClient.ConnectionTimeoutSeconds)
                                {
                                    DeactivateCometWaitRequest(processRequest[i]);
                                    CometMessage timeoutMessage = new CometMessage()
                                    {
                                        MessageId = 0,
                                        Tip       = "aspNetComet.timeout",
                                        Content   = null
                                    };
                                    processRequest[i].Result.CometMessages = new CometMessage[] { timeoutMessage };
                                    this.QueueCometWaitRequest_Finished(processRequest[i]);
                                }
                                else
                                {
                                    // 这里是消息响应的根本,响应给客户端的是 消息数组
                                    CometMessage[] messages = CheckForServerPushMessages(processRequest[i]);

                                    if (messages != null && messages.Length > 0)
                                    {
                                        processRequest[i].Result.CometMessages = messages;
                                        DeactivateCometWaitRequest(processRequest[i]);
                                        QueueCometWaitRequest_Finished(processRequest[i]);
                                    }
                                }
                            }
                            else
                            {
                                // 长连接 连接超时,断网或者 离线
                                this.CheckForIdleCometWaitRequest(processRequest[i], cometClient);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (processRequest[i].Active)
                            {
                                //  ok, this one has screwed up, so
                                //  we need to dequeue the request from ASP.NET, basically disable it and return
                                //  dequeue the request
                                DeactivateCometWaitRequest(processRequest[i]);

                                //  get the message
                                CometMessage errorMessage = new CometMessage()
                                {
                                    MessageId = 0,
                                    Tip       = "aspNetComet.error",
                                    Content   = ex.Message
                                };

                                //
                                //  ok, we we timeout the message
                                processRequest[i].Result.CometMessages = new CometMessage[] { errorMessage };
                                //  call the message
                                QueueCometWaitRequest_Finished(processRequest[i]);
                            }
                            else
                            {
                                //  this is not an active request, so we dequeue it from the
                                //  thread
                                DequeueCometWaitRequest(processRequest[i].ClientPrivateToken);
                            }
                        }
                    }
                }
            }
        }