我的Python3服务器代码:
import zmq
context = zmq.Context()
router = context.socket(zmq.ROUTER)
router.bind("tcp://*:1337")
router.send_multipart([b'build_0', b''])
while True:
pass
我的C#客户端代码:
/// <summary>
/// Receive data from server.
/// </summary>
private void RecvFromServer()
{
// Init the socket.
recvFromServerSocket = new DealerSocket();
recvFromServerSocket.Options.Identity = StringToByteArray("build_0");
recvFromServerSocket.Connect(NETWORK_PREFIX + serverAddress + PORT_INFIX + recvFromServerPort);
NetMQMessage frames = new NetMQMessage();
// When the socket is ready, receive a multipart message.
recvFromServerSocket.ReceiveReady += (o, s) =>
{
onBeginListenToServer?.Invoke(ref BenchmarkData.benchmark.began_to_listen_to_server);
frames = s.Socket.ReceiveMultipartMessage();
foreach (NetMQFrame frame in frames)
{
Debug.Log(frame.ConvertToString());
messagesFromServer.Enqueue(frame.ConvertToString());
}
frames.Clear();
onReceivedMessage?.Invoke(ref BenchmarkData.benchmark.received_from_server);
};
// Init the poller.
recvFromServerPoller = new NetMQPoller { recvFromServerSocket };
// Run the poller.
recvFromServerPoller.Run();
}
现在,C#客户端将不会接收到该消息。
但是,如果我将Python套接字设置为zmq.DEALER
,则客户端将接收到该消息,但会将build_0
视为多部分消息的一部分,而不是发送者ID。我需要实际接受该ID,因为会有多个客户端。身份为build_0
的Python经销商套接字可以与服务器示例进行通信。
如何设置NetMQ DealerSocket以在正确使用身份的同时接受来自pyzmq路由器套接字的任何消息?