public IStreamImplementation GetMsgStreamImpl(JdCometStreamRequest cometReq) { if (cometReq != null) { cometReq.GetConnectListener().OnBeforeConnect(); } JdDictionary param = new JdDictionary(); param.Add(StreamConstants.PARAM_APPKEY, cometReq.GetAppkey()); if (!String.IsNullOrEmpty(cometReq.GetUserId())) { param.Add(StreamConstants.PARAM_USERID, cometReq.GetUserId()); } if (!String.IsNullOrEmpty(cometReq.GetConnectId())) { param.Add(StreamConstants.PARAM_CONNECT_ID, cometReq.GetConnectId()); } param.Add(StreamConstants.PARAM_TIMESTAMP, DateTime.Now.Ticks); IDictionary<string, string> otherParam = cometReq.GetOtherParam(); if (otherParam != null && otherParam.Count > 0) { IEnumerator<KeyValuePair<string, string>> kvps = otherParam.GetEnumerator(); while (kvps.MoveNext()) { param.Add(kvps.Current.Key, kvps.Current.Value); } } string sign = null; try { sign = JdUtils.SignJdRequest(param, cometReq.GetSecret(), true); if (String.IsNullOrEmpty(sign)) { throw new Exception("Get sign error"); } } catch (Exception e) { throw e; } param.Add(StreamConstants.PARAM_SIGN, sign); HttpClient httpClient = new HttpClient(conf, param); HttpResponse response; response = httpClient.Post(); return currentStreamImpl = new MessageStreamImpl(msgConsumeFactory, response, cometMessageListener, this); }
private void StartConsumeThread(JdCometStreamRequest cometReq) { IStreamImplementation stream = null; try { stream = GetMsgStreamImpl(cometReq); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnect(); } } catch (JdCometSysErrorException e) { bsJd = true; logger.Error(e.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnSysErrorException(e); } } catch (Exception ex) { bsJd = true; logger.Error(ex.Message); if (cometReq.GetConnectListener() != null) { cometReq.GetConnectListener().OnConnectError(ex); } } lastStartConsumeThread = DateTime.Now.Ticks; Thread JdCometStreamConsumeThread = new Thread(delegate() { JdCometStreamConsume(lastStartConsumeThread, ref bsJd, stream, cometReq.GetConnectListener()); }); JdCometStreamConsumeThread.Name = "Jd-stream-consume-thread" + cometReq.GetConnectId(); JdCometStreamConsumeThread.Start(); }