/// <summary> /// 先只实现群聊,返回是否请求到了消息 /// </summary> public void DoChatSession(ICometRequest cometRequest, IEnumerable<ChatMessageModel> messages, FinishCallback cb) { CometAsyncResult request = cometRequest as CometAsyncResult; var newMessages = messages.Where(p => p.Id > request.Request.lastId); if (newMessages.Count() > 0) { request.Response.Message = newMessages; cometRequest.IsCompeled = true; } else { request.Response.Message =new List<ChatMessageModel>(); } if(cb!=null) cb(cometRequest); }
public void DoChatSession(ICometRequest cometRequest,IEnumerable<ChatMessageModel> messages, FinishCallback cb) { HttpCometRequest request = cometRequest as HttpCometRequest; string outStr=string.Empty; var newMessages = messages.Where(p => p.Id > request.LastId); if (newMessages.Count() > 0) { outStr = newMessages.Count().ToString() + "new message"; cometRequest.IsCompeled = true; } request.OutPutMessage = outStr; if (cb != null) cb(cometRequest); }
/// <summary> /// 以新消息触发 队列请求处理 /// </summary> void HandleEventTriggerMode(ICometRequest[] requests) { //1s超时进入轮询 SessionWaitHandle.WaitOne(1000); ChatMessageModel[] chatMessages; lock (MessageSyncRoot) { chatMessages = CometChatMessage.ToArray(); //内存中值保留前20条记录,避免查询耗时 CometChatMessage = CometChatMessage.Take(20).ToList(); } foreach (var request in requests) { SessionManager.DoChatSession(request, chatMessages, FinishCometRequest); } }
/// <summary> /// 完成请求删除节点 /// </summary> public void DeQueueCometRequest(ICometRequest request) { lock (RequestSyncRoot) { CometRequestList.Remove(request); } }
/// <summary> /// 将请求加入线程处理队列 /// </summary> public void EnQueueCometRequest(ICometRequest request) { request.CometConcurrentCount = RequestCount; //需要立即处理请求,如果有数据及立即返回,无数据才加入队列 HandleCurrentRequest(request); if (request.IsCompeled) { return; } //将请求加入队列处理 lock (RequestSyncRoot) { CometRequestList.AddFirst(request); //通知线程开始工作 ThreadWaitHandle.Set(); } }
/// <summary> /// 完成长连接处理 /// </summary> public void FinishCometRequest(ICometRequest request) { if (request.IsCompeled||(DateTime.Now - request.BeginTime).TotalSeconds >= RequestTimeOut) { DeQueueCometRequest(request); request.FinishCometRequest(); } }
/// <summary> /// 立即处理请求(返回时候得到处理) /// </summary> void HandleCurrentRequest(ICometRequest request) { lock (MessageSyncRoot) { //处理一个请求,不对MessageList copy了 SessionManager.DoChatSession(request,CometChatMessage ,null ); if(request.IsCompeled) { request.FinishCometRequest(); } } }
/// <summary> /// 单轮询模式处理,定时检查消息队列 /// </summary> void HandlePollingMode(ICometRequest[] requests) { ChatMessageModel[] chatMessages; lock (MessageSyncRoot) { chatMessages = CometChatMessage.ToArray(); //内存中值保留前20条记录,避免查询耗时 CometChatMessage = CometChatMessage.Take(20).ToList(); } foreach (var request in requests) { SessionManager.DoChatSession(request, chatMessages, FinishCometRequest); } //定时扫描 Thread.Sleep(200); }
/// <summary> /// 把长连接队列 /// </summary> /// <param name="result"></param> public static void QueueCometRequest(ICometRequest result) { lock (SyncRoot) { if (AssignRequestThreadIndex == ThreadCount) { AssignRequestThreadIndex = 0; } CometThreads[AssignRequestThreadIndex].EnQueueCometRequest(result); AssignRequestThreadIndex++; } }