【发布时间】:2016-07-16 16:03:19
【问题描述】:
我正在尝试定期发送“hello world!”向 Ratchet 教程中连接到聊天服务器的所有客户端发送消息
我将在这里发布所有代码: Chat.php:
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
public $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
//this worked but I don't want this behaviour
public function onMessage(ConnectionInterface $from, $msg) {
/*$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}*/
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
聊天服务器.php:
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new Chat(),
8080
);
$server->run();
为了测试我理解了多少文档,我在服务器的循环中添加了一个计时器
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new Chat(),
8080
);
// My code here
$server->loop->addPeriodicTimer(5, function () {
echo "custom loop timer working !";
});
$server->run();
并且在启动服务器后每五秒输出一次该字符串效果很好。
现在我尝试这样做,尝试向存储在本教程中称为 Chat 的 MessageComponentInterface 中的客户端发送消息
$server->loop->addPeriodicTimer(5, function () {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
但是我得到 $server->app 是 NULL 这可能是因为我现在在 function() 块内。当谈到面向对象的 PHP 时,我不是专家,这个小项目将肯定对我有很大帮助。
如何访问计时器内服务器的MessageComponentInterface 称为app 属性,然后将数据发送到存储在其中的客户端?
【问题讨论】:
-
在
function ()之后添加use ($server) -
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.secure.php.net/manual/en/functions.anonymous.php -
$server在闭包中为 NULL,因为它尚未在函数范围内定义。在此处阅读更多信息secure.php.net/manual/en/language.variables.scope.php -
@CharlotteDunois 这就像一个魅力。我一直想知道如何做到这一点,我什至曾经在 JS 中遇到过这样的问题,不得不重做很大一部分来防止这种情况发生。 (现在想知道这是否适用于 JS:p)。您能否添加一个答案以便我接受?
-
在 JS 中,所有全局变量都在子作用域中可用。虽然更好的选择是将其作为参数传递,而不是将所有相关变量定义为全局变量。
标签: php web-services websocket ratchet