問題描述
我正在嘗試定期發送hello world!"從 Ratchet 教程向所有連接到聊天服務器的客戶端發送消息
I'm trying to periodically send a "hello world!" message to all clients connected to the chat-server from the Ratchet tutorial
我將在這里發布所有代碼:聊天.php:
I will post all of the code here: Chat.php:
<?php
namespace MyApp;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
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})
";
}
//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' . "
"
, $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
";
}
public function onError(ConnectionInterface $conn, Exception $e) {
echo "An error has occurred: {$e->getMessage()}
";
$conn->close();
}
}
聊天服務器.php:
<?php
use RatchetServerIoServer;
use MyAppChat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new Chat(),
8080
);
$server->run();
為了測試我理解了多少文檔,我在服務器的循環中添加了一個計時器
To test how much of the docs I understood , I added a timer to the server's loop
<?php
use RatchetServerIoServer;
use MyAppChat;
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();
并且它在啟動服務器后每五秒輸出一次該字符串工作正常.
and it worked fine outputting that string every five seconds after starting the server.
現在我嘗試這樣做,嘗試向存儲在 MessageComponentInterface 中的客戶端發送一條消息,該接口稱為教程中的 Chat
Now I tried doing it like so, trying to send a message to clients stored in the MessageComponentInterface called Chat from the tutorial
$server->loop->addPeriodicTimer(5, function () {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
但我得到 $server->app 為 NULL 這可能是因為我現在在 function() 塊中.當談到面向對象的 PHP 時,我不是專家,這個小項目將肯定對我有很大幫助.如何在計時器內訪問名為 app
的服務器的 MessageComponentInterface
屬性,然后將數據發送到存儲在其中的客戶端?
But I'm getting that $server->app is NULL which is probably because I'm now inside the function() block .I'm not an expert when it comes to Object oriented PHP, and this little project will sure help me a lot.
How can I access the MessageComponentInterface
called app
property of the server inside the timer and then send data to the clients stored in there?
推薦答案
$server
沒有在函數作用域中定義,父作用域的變量不會被繼承到子作用域默認.閉包可以通過使用 use
語言結構從父作用域繼承變量.
$server
isn't defined in the function scope and variables from the parent scope don't get inherited to the child scope by default. Closures can inherit variables from the parent scope by using the use
language construct.
$server->loop->addPeriodicTimer(5, function () use ($server) {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
關于匿名函數(閉包)的更多信息:https://secure.php.net/manual/en/functions.anonymous.php
有關變量范圍的更多信息:https://secure.php.net/manual/en/language.variables.scope.php
More information about anonymous functions (closures): https://secure.php.net/manual/en/functions.anonymous.php
More information about variables scope: https://secure.php.net/manual/en/language.variables.scope.php
這篇關于定期向 Ratchet 中的客戶端發送消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!