// 在异步调用结束的时候,调用此业务方法 public void EndSubscribe(IAsyncResult result) { this.DebugWriteThreadInfo("EndSubscribe"); CometAsyncResult cometAsyncResult = result as CometAsyncResult; if (cometAsyncResult != null) { try { CometMessage[] messages = cometAsyncResult.CometMessages; if (messages != null && messages.Length > 0) { /* 此方式序列化产生的字符串不是常规JSon定义字符串,暂时不采用 * List<Type> knownTypes = new List<Type>(); * foreach (CometMessage message in messages) * { * if (message.Contents != null) * { * Type knownType = message.Contents.GetType(); * * if (!knownTypes.Contains(knownType)) * { * knownTypes.Add(knownType); * } * } * } * DataContractJsonSerializer serializer = new DataContractJsonSerializer(messages.GetType(),knownTypes); * serializer.WriteObject(((CometAsyncResult)result).Context.Response.OutputStream, messages); */ var strResponse = Newtonsoft.Json.JsonConvert.SerializeObject(messages); ((CometAsyncResult)result).Context.Response.Write(strResponse); } } catch (Exception ex) { this.WriteErrorToResponse(((CometAsyncResult)result).Context, new { From = "system", Message = ex.Message }); } } }
/// <summary> /// /// </summary> /// <param name="clientPrivateToken"></param> /// <param name="lastMessageId"></param> /// <param name="context"></param> /// <param name="callback"></param> /// <param name="state"></param> public CometWaitRequest(string clientPrivateToken, long lastMessageId, HttpContext context, AsyncCallback callback, object state) { ClientPrivateToken = clientPrivateToken; LastMessageId = lastMessageId; Result = new CometAsyncResult(context, callback, state); }