标题说明了一切。这是我的代码:
(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”功能放置了一个发布到该 channel 的按钮。这有效。
从上面可以看出,我没有将 ZeroiMQ 用于 IPC,因为我是在 Windows 机器上开发的,在 PHP 5 上。AFAIK,没有适用于 PHP5 的 ZeroMQ 绑定(bind)。
我求助于使用裸套接字。它们工作得同样漂亮,我可以在 CLI 上看到消息确实到达了这个特定的脚本。
“onUpdate”函数确实通过 CLI 再次被调用和确认。
我之前曾尝试使用 URL“http:\example.com\Prime_mover”,但当它不起作用时,出于绝望,我尝试了字符串“Prime_mover”。你现在可能正在摇头——我知道,这样不行。
我也尝试使用 $topica 作为数组,但不起作用。我想这里最重要的问题是,$topic 是什么类型的对象,为什么一个简单的字符串不能在它的位置工作?我在这里遗漏了什么?它是如何正确“构建”的?
最佳答案
$topic = 'Prime_mover';
$topic->broadcast($entry);
$topic 是一个字符串!而且它没有任何方法。
关于wamp - Ratchet WAMP $topic 错误 :Call to a member function broadcast() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21811539/