【发布时间】:2019-07-14 09:23:07
【问题描述】:
我正在尝试使用 C# UWP 中的 NetMQ 将消息传递给 python。
python 充当订阅者,C# 充当发布者。
当我使用 C# .Net Core 时,我可以看到消息到达 python 订阅者,但是当我使用 C# UWP 时,没有任何反应,尽管代码完全相同,我可以看到 Publisher 正在发送消息。
python中的代码:(工作中)
import zmq
import time
def subscribe():
port = "6789"
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:%s" % port)
topicfilter = "abcde"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
while True:
string = socket.recv()
print string
subscribe()
.Net Core 中的代码:(工作中)
using System.Threading;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;
namespace Examples
{
static partial class Program
{
public static void Main(string[] args)
{
Publisher();
}
public static void Publisher()
{
Task.Run(async () =>
{
using (var pubSocket = new PublisherSocket())
{
pubSocket.Bind("tcp://*:6789");
for (var i = 0; i < 10; i++)
{
pubSocket.SendFrame("abcde" + i.ToString());
Thread.Sleep(1000);
}
}
});
}
}
}
但是 UWP 中的代码(不工作):
using NetMQ;
using NetMQ.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System;
namespace test_NetMQ_UWP
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
DataContext = this;
}
// this event happen when I click on a button in MainPage.xaml
private void Publisher_Click(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
using (var pubSocket = new PublisherSocket())
{
pubSocket.Bind("tcp://*:6789");
for (var i = 0; i < 10; i++)
{
pubSocket.SendFrame("abcde" + i.ToString());
Thread.Sleep(1000);
}
}
});
}
}
}
我做错了什么?
【问题讨论】:
-
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)zmq 选项,Python代码中必须放在连接前,试试看。 -
谢谢,但这并不能解决问题。