【发布时间】:2014-02-27 20:30:43
【问题描述】:
标题说明了一切。这是我的代码:
(Shell script)
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Chat;
//Receiving IPC messages:
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
$pusher = new MyApp\Chat;
echo date('H:i:s'). "!!IPC Connection opened!!\n";
$conn->on('data', array($pusher, 'onUpdate'));
});
$socket->listen(1337, '127.0.0.1'); //Binding to our IP so remotes can't connect.
echo "%%% IPC listener started succesfully. %%%\n%%%\n";
// WebSocket server:
//here the code is identical to that on the Ratchet 'push-server' tutorial
...以及“onUpdate”功能...
public function onUpdate($entry)
{
echo(date('H:i:s'). ": !<<---IPC Data Received.DATA:::". $entry. ":::--->>!\n");
$topic = 'Prime_mover';
$topic->broadcast($entry);
}
...“onPublish”函数:
public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible )
{
echo $topic. "\n";
//echo implode(array_keys($topic)). "\n";
$channel = $topic->getId();
if($topic->broadcast($event))
{
echo(date('H:i:s') . ": ***A client has published ###" .
implode('', $event) . "### to (((". $channel. ")))***\n");
} else {
echo(date('H:i:s'). ": !!<--An error occured during publish-->!!\n");
}
}
客户端代码很简单。
我确信错误(如果有的话)(我已经在这里待了大约 10 个小时)不会驻留在那里。
我通过控制台确认浏览器确实订阅了“Prime_mover”。这也显示在 CLI 上。此外,我通过“onPublish”功能放置了一个发布到该频道的按钮。这行得通。
如上所示,我没有将 ZeroiMQ 用于 IPC,因为我正在 Windows 机器上使用 PHP 5 进行开发。AFAIK,不存在适用于 PHP5 的 ZeroMQ 绑定。
我求助于使用裸插座。它们的工作同样出色,我可以在 CLI 上看到消息确实到达了这个特定的脚本。
“onUpdate”函数确实会通过 CLI 再次调用和确认。
我之前尝试过使用 URL“http:\example.com\Prime_mover”,当它不起作用时,出于绝望,我尝试了字符串“Prime_mover”。你现在可能正在摇头——我知道,那样不行。
我也尝试将 $topica 用作数组,但不起作用。我想这里最重要的问题是,$topic 是什么类型的对象,为什么不能用一个简单的字符串代替它?是吗?这里遗漏了什么?它是如何正确“构造”的?
【问题讨论】:
-
我也对此感到困惑。 $topic是一个字符串它是怎么得到广播方法的???
标签: wamp publish-subscribe ratchet