有人可以帮我找到一些示例代码,以便使用您选择的任何.NET语言使用MSMQ发送和接收多播消息。我搜索了一下,发现发送了:

MessaegQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")


我试图用接收做同样的想法:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Receive();


但我什么也没得到。谁能显示一些有关如何接收多播消息的示例代码?还是我发错了?

最佳答案

所以我想通了。

要发送多播消息:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")


接收多播消息:

这有点棘手,因为您无法订阅多播地址。您需要做的是创建一个队列,最好是创建一个私有队列,该队列将附加到您要监视的多播地址,然后侦听您创建的多播地址INSTEAD的私有队列。像这样:

  Dim privMulticastQueue As String = GetPrivateQueueForMulticastAddress("formatname:multicast=234.1.1.1:8081")
  Dim msgq as MessageQueue = GetMessageQueue(privMulticastQueue)
  msgq.MulticastAddress = GetMulticastAddress(destination)
  msgq.Label = "Private Queue for receiving messages from: " & destination
  msgq.Receive()


和一些支持方法(可能有更好的编写方法,因此可以随时进行纠正,但这是我的第一个建议):

 Private Function GetPrivateQueueForMulticastAddress(ByVal dest As String) As String
    Dim privateQ As String = GetMulticastAddress(dest).Replace(".", "_").Replace(":", "_")
    Return ".\Private$\" & privateQ
 End Function

Private Function GetMulticastAddress(ByVal dest As String) As String
    Return dest.Split("=")(1)
End Function

Private Function GetMessageQueue(ByVal dest As String) As MessageQueue
      Try
           If Not MessageQueue.Exists(dest) Then
             MessageQueue.Create(dest)
            End If

            Dim msgq As MessageQueue = New MessageQueue(dest)
            Return msgq
      Catch ex As Exception
            Throw New EMGException("Failed while trying to use destination: " & dest, ex)
      End Try

End Function

关于c# - 如何使用MSMQ发送/接收多播消息?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28773194/

10-13 06:11