有人可以帮我找到一些示例代码,使用您选择的任何.NET语言,使用MSMQ发送和接收多播消息。我四处搜索,看到发送是:
MessaegQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")
我尝试用Receive做同样的想法:
MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Receive();
但我一无所获。任何人都可以展示如何接收多播消息的示例代码吗?还是我发错了?
答案 0 :(得分:2)
所以我明白了。
发送多播消息:
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