定期向 Ratchet 中的客户端发送消息
Periodically sending messages to clients in Ratchet
我正在尝试定期向所有连接到 Ratchet 教程中的聊天服务器的客户端发送 "hello world!" 消息
我将post这里的所有代码:
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();
}
}
聊天-server.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();
并且在启动服务器后每五秒输出一次该字符串效果很好。
现在我尝试这样做,尝试向存储在 MessageComponentInterface 中的客户端发送一条消息,调用教程中的 Chat
$server->loop->addPeriodicTimer(5, function () {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
但是我发现 $server->app 是 NULL,这可能是因为我现在在 function() 块中。我不是面向对象方面的专家 PHP ,这个小项目肯定会对我有很大帮助。
我怎样才能在定时器内部访问服务器的MessageComponentInterface
调用app
属性,然后将数据发送到存储在那里的客户端?
$server
未在函数作用域中定义,默认情况下父作用域中的变量不会继承到子作用域。闭包可以使用 use
语言结构从父作用域继承变量。
$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
经过一些更新后,可以在 MessageHandler 中访问客户端连接
$port = 3001;
$handler = new MessageHandler();
$server = IoServer::factory(
new HttpServer(
new WsServer(
handler
)
),
$port
);
$server->loop->addPeriodicTimer(0.1, function () use ($handler) {
handler->doStuff();
});
$server->run();
可以在此处找到 MessageHandler。 doStuff方法应该在这个class:
中实现
https://github.com/leorojas22/symfony-websockets/blob/master/src/Websocket/MessageHandler.php
我正在尝试定期向所有连接到 Ratchet 教程中的聊天服务器的客户端发送 "hello world!" 消息
我将post这里的所有代码: 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();
}
}
聊天-server.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();
并且在启动服务器后每五秒输出一次该字符串效果很好。
现在我尝试这样做,尝试向存储在 MessageComponentInterface 中的客户端发送一条消息,调用教程中的 Chat
$server->loop->addPeriodicTimer(5, function () {
foreach ($server->app->clients as $client) {
$client->send("hello client");
}
});
但是我发现 $server->app 是 NULL,这可能是因为我现在在 function() 块中。我不是面向对象方面的专家 PHP ,这个小项目肯定会对我有很大帮助。
我怎样才能在定时器内部访问服务器的MessageComponentInterface
调用app
属性,然后将数据发送到存储在那里的客户端?
$server
未在函数作用域中定义,默认情况下父作用域中的变量不会继承到子作用域。闭包可以使用 use
语言结构从父作用域继承变量。
$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
经过一些更新后,可以在 MessageHandler 中访问客户端连接
$port = 3001;
$handler = new MessageHandler();
$server = IoServer::factory(
new HttpServer(
new WsServer(
handler
)
),
$port
);
$server->loop->addPeriodicTimer(0.1, function () use ($handler) {
handler->doStuff();
});
$server->run();
可以在此处找到 MessageHandler。 doStuff方法应该在这个class:
中实现https://github.com/leorojas22/symfony-websockets/blob/master/src/Websocket/MessageHandler.php