您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman常用组件-channel分佈式通訊組件-channelClient-例子-集群推送
发布时间:2021-12-19 16:18:20编辑:雪饮阅读()
(
要求
Workerman
版本
>=3.3.0)
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// 初始化一个Channel服务端
$channel_server = new Channel\Server('0.0.0.0', 2206);
Worker::runAll();
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// websocket服务端
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->count=2;
$worker->name = 'pusher';
$worker->onWorkerStart = function($worker)
{
// Channel客户端连接到Channel服务端
Channel\Client::connect('192.168.43.170', 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(TcpConnection $connection)use($worker)
{
$msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
echo $msg;
$connection->send($msg);
};
Worker::runAll();
服務端啓動:
這裏httpServer負責發佈廣播或向某個進程中的某個websocket客戶端發送消息 然後訪問如:
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// 用来处理http请求,向任意客户端推送数据,需要传workerID和connectionID
$http_worker = new Worker('http://0.0.0.0:4237');
$http_worker->name = 'publisher';
$http_worker->onWorkerStart = function()
{
Channel\Client::connect('192.168.43.170', 2206);
};
$http_worker->onMessage = function(TcpConnection $connection, $request)
{
// 兼容workerman4.x
if (!is_array($request)) {
$_GET = $request->get();
}
$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();
<html>
<head>
<script>
ws1 = new WebSocket("ws://192.168.43.242:4236");
ws1.onmessage = function(e) {
alert("ws1收到服务端的消息:" + e.data);
};
</script>
</head>
<body>
<div class="container">
<h1>恭喜, 站点创建成功!</h1>
<h3>这是默认index.html,本页面由系统自动生成</h3>
<ul>
<li>本页面在FTP根目录下的index.html</li>
<li>您可以修改、删除或覆盖本页面</li>
<li>FTP相关信息,请到“面板系统后台 > FTP” 查看</li>
</ul>
</div>
</body>
</html>
<html>
<head>
<script>
ws2 = new WebSocket("ws://192.168.43.65:4236");
ws2.onmessage = function(e) {
alert("ws2收到服务端的消息:" + e.data);
};
</script>
</head>
<body>
<div class="container">
<h1>恭喜, 站点创建成功!</h1>
<h3>这是默认index.html,本页面由系统自动生成</h3>
<ul>
<li>本页面在FTP根目录下的index.html</li>
<li>您可以修改、删除或覆盖本页面</li>
<li>FTP相关信息,请到“面板系统后台 > FTP” 查看</li>
</ul>
</div>
</body>
</html>
关键字词:workerman,channel,channelClient,集群,推送
上一篇:workerman常用组件-channel分佈式通訊組件-channelClient-unsubscribe
下一篇:workerman常用组件-channel分佈式通訊組件-channelClient-例子-分組發送(聊天組應用場景)
相关文章
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelSer
- workerman常用组件-channel分佈式通訊組件
- workerman常用组件-GlobalData变量共享组件-GlobalDat
- workerman常用组件-GlobalData变量共享组件-GlobalDat
- workerman常用组件-GlobalData变量共享组件-GlobalDat
- workerman常用组件-GlobalData变量共享组件-GlobalDat