/// <summary> /// 处理服务器请求 /// </summary> /// <param name="request"></param> /// <param name="timeout"></param> /// <returns></returns> private object CheckForServerPushEvent(CometWaitRequest request, bool timeout) { if (timeout) { return("Response from: " + Thread.CurrentThread.ManagedThreadId.ToString()); } List <MessageModel> listMessage = new List <MessageModel>(); List <NoticeModel> listNotice = new List <NoticeModel>(); //取用户消息信息 listMessage = GetMessage(request.SendUserId); //取用户好友相关通知 listNotice = GetNotice(request.SendUserId); if (listMessage.Count == 0 && listNotice.Count == 0) { return(null); } ArgumentModel modelArgument = new ArgumentModel(); modelArgument.listMessage = listMessage; modelArgument.listNotice = listNotice; return(modelArgument); }
/// <summary> /// 开始线程处理 /// </summary> private void QueueCometWaitRequest_WaitCallback() { try { while (true) { CometWaitRequest[] processRequest; lock (this.state) { processRequest = waitRequests.ToArray(); } Thread.Sleep(500); for (int i = 0; i < processRequest.Length; i++) { CometWaitRequest cometWaitRequest = processRequest[i]; // 超时则从线程队列中移除对该客户端的监听 if (DateTime.Now.Subtract(cometWaitRequest.DateTimeAdded).TotalSeconds >= 20) { //从当前线程队列移除监听客户端 DequeueCometWaitRequest(cometWaitRequest); this.QueueCometWaitRequest_Finished(cometWaitRequest, null); } else { object serverPushEvent = this.CheckForServerPushEvent(processRequest[i], false); if (serverPushEvent != null) { ArgumentModel modelArgument = (ArgumentModel)serverPushEvent; this.QueueCometWaitRequest_Finished(cometWaitRequest, modelArgument); //从当前线程队列移除监听客户端 DequeueCometWaitRequest(cometWaitRequest); } } Thread.Sleep(5); } } } catch (Exception ex) { log.Warn(ex); QueueCometWaitRequest_WaitCallback(); } }
/// <summary> /// 当前客户端监听任务完成执行函数 /// </summary> /// <param name="target"></param> private void QueueCometWaitRequest_Finished(object target, ArgumentModel modelArgument) { CometWaitRequest request = target as CometWaitRequest; request.execute.BeginInvoke(modelArgument, null, null); }