コード例 #1
0
 public bool Publish(string topic, ISocketData sendDto)
 {
     if (string.IsNullOrWhiteSpace(topic))
     {
         throw new ArgumentNullException(topic);
     }
     try
     {
         var sendVal = JsonConvert.SerializeObject(sendDto);
         sendVal = topic + "/" + sendVal;
         var valBytes = Encoding.UTF8.GetBytes(sendVal);
         // var isSend = PublisherSocket.SendMoreFrame(topic).TrySendFrameBytes(sendVal);
         var isSend = PublisherSocket.TrySendFrameBytes(sendVal);
         if (isSend)
         {
             LogWrite.Error("[发布消息]消息已广播出去:" + sendVal);
         }
         return(isSend);
     }
     catch (Exception e)
     {
         LogWrite.Error(e);
         return(false);
     }
 }
コード例 #2
0
 public T RequestReceive <T>(IRequestData sendDto) where T : IResponseData
 {
     try
     {
         var str = RequestReceive(sendDto);
         return(JsonConvert.DeserializeObject <T>(str));
     }
     catch (Exception e)
     {
         LogWrite.Error(e);
         return(default(T));
     }
 }
コード例 #3
0
 public T ReceiveSubscriber <T>(string topic) where T : ISocketData
 {
     try
     {
         var receive = ReceiveSubscriber(topic);
         return(JsonConvert.DeserializeObject <T>(receive));
     }
     catch (Exception e)
     {
         LogWrite.Error(e);
         return(default(T));
     }
 }
コード例 #4
0
 public void ResponseReceive(Func <string, IResponseData> ReceiveCallback)
 {
     try
     {
         var receiveStr = ResponseSocket.ReceiveFrameBytesSTR();
         LogWrite.Error("[收消息]收到客户端消息:" + receiveStr);
         var responseInfo = ReceiveCallback(receiveStr);
         ResponseSocket.TrySendFrameBytes(responseInfo);
     }
     catch (Exception e)
     {
         LogWrite.Error(e);
     }
 }
コード例 #5
0
 private string RequestReceive(IRequestData sendDto)
 {
     lock (lockObj)
     {
         try
         {
             RequestSocket.TrySendFrameBytes(sendDto);
             var receive = RequestSocket.ReceiveFrameBytesSTR();
             return(receive);
         }
         catch (Exception e)
         {
             LogWrite.Error(e);
             return(string.Empty);
         }
     }
 }
コード例 #6
0
 public string ReceiveSubscriber(string topic)
 {
     try
     {
         if (SubscriberSocket == null)
         {
             SubscriberSocket = NetMQFactory.CreateSubscriberSocket(_publisherUrl, topic);
         }
         //string messageTopicReceived = SubscriberSocket.ReceiveFrameString();
         var result = SubscriberSocket.ReceiveFrameBytesSTR();
         LogWrite.Error("[订阅消息]收到消息:" + result);
         return(result.Substring(topic.Length + 1));
     }
     catch (Exception ex)
     {
         LogWrite.Error(ex);
         return(string.Empty);
     }
 }