PHP 的 ZeroMQ XPub-XSub:消息永远不会到达代理
ZeroMQ XPub-XSub with PHP: Messages never arrive at proxy
我正在尝试使用带有 ZeroMQ 的代理和 PHP as it is described in the guide in Figure 13. The setup is the same as described here: how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(jzmq) 3.xx
来实现发布订阅
subscriber.php
<?php
$context = new ZMQContext();
$sub = new ZMQSocket($context, ZMQ::SOCKET_SUB);
$sub->connect("tcp://127.0.0.1:5000");
$sub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, 'Hello');
$msg = $sub->recv();
echo "got $msg";
publisher.php
<?php
$context = new ZMQContext();
$pub = new ZMQSocket($context, ZMQ::SOCKET_PUB);
$pub->connect("tcp://127.0.0.1:6000");
while (1) {
echo "publishing";
$pub->send("Hello World");
sleep(1);
}
proxy.php
<?php
$context = new ZMQContext();
$frontend = new ZMQSocket($context, ZMQ::SOCKET_XSUB);
$frontend->bind("tcp://127.0.0.1:6000");
$backend = new ZMQSocket($context, ZMQ::SOCKET_XPUB);
$backend->bind("tcp://127.0.0.1:5000");
$device = new ZMQDevice($frontend, $backend);
$device->run();
如果我启动所有三个 PHP 脚本(首先是代理,然后是发布者,然后是订阅者),则没有消息到达该订阅者。
为了查看是否有任何消息到达代理,我尝试在代理上手动接收消息:
while (true) {
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xpub";
}
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xsub";
}
}
Stack Overflow 上有几个相关问题:
- How to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(C++)? - 我正在使用答案中描述的代理,但我仍然没有收到任何消息。因此这个问题不适用
- how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(jzmq) 3.xx回答的人说问题是发布者的消息没有转发给订阅者。但是,我根本没有收到发布者的任何消息。
我错过了什么?
[PROXY] 也需要设置主题过滤器:
$frontend->send( chr(1) + "" ); /* XSUBSCRIBE to { ANY == "" } topic incoming */
( For ZeroMQ API-Ref.: >>> ZeroMQ API documentation )
我正在尝试使用带有 ZeroMQ 的代理和 PHP as it is described in the guide in Figure 13. The setup is the same as described here: how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(jzmq) 3.xx
来实现发布订阅subscriber.php
<?php
$context = new ZMQContext();
$sub = new ZMQSocket($context, ZMQ::SOCKET_SUB);
$sub->connect("tcp://127.0.0.1:5000");
$sub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, 'Hello');
$msg = $sub->recv();
echo "got $msg";
publisher.php
<?php
$context = new ZMQContext();
$pub = new ZMQSocket($context, ZMQ::SOCKET_PUB);
$pub->connect("tcp://127.0.0.1:6000");
while (1) {
echo "publishing";
$pub->send("Hello World");
sleep(1);
}
proxy.php
<?php
$context = new ZMQContext();
$frontend = new ZMQSocket($context, ZMQ::SOCKET_XSUB);
$frontend->bind("tcp://127.0.0.1:6000");
$backend = new ZMQSocket($context, ZMQ::SOCKET_XPUB);
$backend->bind("tcp://127.0.0.1:5000");
$device = new ZMQDevice($frontend, $backend);
$device->run();
如果我启动所有三个 PHP 脚本(首先是代理,然后是发布者,然后是订阅者),则没有消息到达该订阅者。
为了查看是否有任何消息到达代理,我尝试在代理上手动接收消息:
while (true) {
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xpub";
}
if ($frontend->recv(ZMQ::MODE_DONTWAIT)) {
echo "received message from xsub";
}
}
Stack Overflow 上有几个相关问题:
- How to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(C++)? - 我正在使用答案中描述的代理,但我仍然没有收到任何消息。因此这个问题不适用
- how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(jzmq) 3.xx回答的人说问题是发布者的消息没有转发给订阅者。但是,我根本没有收到发布者的任何消息。
我错过了什么?
[PROXY] 也需要设置主题过滤器:
$frontend->send( chr(1) + "" ); /* XSUBSCRIBE to { ANY == "" } topic incoming */
( For ZeroMQ API-Ref.: >>> ZeroMQ API documentation )