client sends messages that cannot be broadcast globally, but can be broadcast to the current user
if I comment on onWorkerStart, I can broadcast globally
if I don"t comment on onWorkerStart, I can"t broadcast globally, but I can reply to the current client
onWorkerStart
.
all the code of the file
<?php
//tcp_l.php
use Workerman\Worker;
require_once __DIR__ . "/Workerman/Autoloader.php";
require_once __DIR__ . "/Workerman/Lib/Timer.php";
require_once __DIR__ . "/Channel/src/Server.php";
require_once __DIR__ . "/Channel/src/Client.php";
define("HEARTBEAT_TIME", 30); // 30
define("CHECK_HEARTBEAT_TIME", 1); //
// Channel
$channel_server = new Channel\Server("0.0.0.0", 2206);
// websocket
$worker = new Worker("websocket://0.0.0.0:4236");
$worker->count=2;
$worker->name = "pusher";
$worker->onWorkerStart = function($worker)
{
// ChannelChannel
Channel\Client::connect("127.0.0.1", 2206);
// id
$event_name = $worker->id;
// worker->id
Channel\Client::on($event_name, function($event_data)use($worker){
$to_connection_id = $event_data["to_connection_id"];
$message = $event_data["content"];
if(!isset($worker->connections[$to_connection_id]))
{
echo "connection not exists\n";
return;
}
$to_connection = $worker->connections[$to_connection_id];
$to_connection->send($message);
});
//
$event_name = "";
//
Channel\Client::on($event_name, function($event_data)use($worker){
$message = $event_data["content"];
foreach($worker->connections as $connection)
{
$connection->send($message);
}
});
};
//
$worker->onConnect = function($connection)use($worker)
{
//
//$msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
$connection->send("Hello,Word!");
};
//
$worker->onMessage = function($connection, $data) {
// connectionlastMessageTime
$connection->lastMessageTime = time();
//
//
//
$event_name = "";
$content = "you just received:".$data;
Channel\Client::publish($event_name, array(
"content" => $content
));
//
//$connection->send("you just send: $data");
};
//
$worker->onClose = function ($connection){
// foreach($text_worker->connections as $conn)
// {
// //$conn->send("user[{$connection->uid}] logout");
// }
};
//
$worker->onWorkerStart = function($worker) {
echo "Worker starting...\n";
// Workerman\Lib\Timer::add(CHECK_HEARTBEAT_TIME,function()use($worker){
// $time_now = time();
// foreach($worker->connections as $connection) {
// // connectionlastMessageTime
// if (empty($connection->lastMessageTime)) {
// $connection->lastMessageTime = $time_now;
// continue;
// }
// //
// if ($time_now - $connection->lastMessageTime > HEARTBEAT_TIME) {
// $connection->close();
// }
// }
// });
};
// httpworkerIDconnectionID
$http_worker = new Worker("http://0.0.0.0:4237");
$http_worker->name = "publisher";
$http_worker->onWorkerStart = function()
{
Channel\Client::connect("127.0.0.1", 2206);
};
$http_worker->onMessage = function($connection, $data)
{
$connection->send("ok");
if(empty($_GET["content"])) return;
// worker
if(isset($_GET["to_worker_id"]) && isset($_GET["to_connection_id"]))
{
$event_name = $_GET["to_worker_id"];
$to_connection_id = $_GET["to_connection_id"];
$content = $_GET["content"];
Channel\Client::publish($event_name, array(
"to_connection_id" => $to_connection_id,
"content" => $content
));
}
//
else
{
$event_name = "";
$content = $_GET["content"];
Channel\Client::publish($event_name, array(
"content" => $content
));
}
};
Worker::runAll();