我正在尝试使用图13中的ZeroMQ和PHP as it is described in the guide使用代理实现发布订阅。设置与此处描述的相同: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上有几个相关的问题:
我错过了什么?
答案 0 :(得分:1)
$frontend->send( chr(1) + "" ); /* XSUBSCRIBE to { ANY == "" } topic incoming */
(适用于ZeroMQ API-Ref。:&gt;&gt;&gt; ZeroMQ API documentation)