public void StartSubscriber <T>(string topic, int Milliseconds = NetmqOptions.SubscriberMillisecond) where T : ISocketData { Task.Factory.StartNew(() => { try { var isCancel = cancellationTokenSource.Token.IsCancellationRequested; var mqTime = new NetMQTimer(TimeSpan.FromMilliseconds(Milliseconds)); mqTime.Elapsed += (sender, e) => { if (isCancel) { e.Timer.Enable = false; return; } var result = _netMQManage.ReceiveSubscriber <T>(topic); OnReceiveSubscriberEvent(result); }; using (var poller = new NetMQPoller { mqTime }) { poller.Run(); } } catch (Exception ex) { LogWrite.Error(ex); } }, cancellationTokenSource.Token); }
public void ResponseReceive(int Milliseconds = NetmqOptions.ResopneMillisecond) { Task.Factory.StartNew((par) => { try { var listen = par as List <PublisherListen>; var isCancel = cancellationTokenSource.Token.IsCancellationRequested; var mqTime = new NetMQTimer(TimeSpan.FromMilliseconds(Milliseconds)); mqTime.Elapsed += (sender, e) => { if (isCancel) { e.Timer.Enable = false; return; } _netMQManage.ResponseReceive((a) => { //收到消息后进行回调处理业务,处理完后在 反馈消息出去 var request = JsonConvert.DeserializeObject <RequestData>(a); listen.ResultListens(request.Id); return(ResultResponse(request)); }); }; using (var poller = new NetMQPoller { mqTime }) { poller.Run(); } } catch (Exception ex) { LogWrite.Error(ex); } }, _publisherListens, cancellationTokenSource.Token); }