PHP + GatewayWorker 实现多个页面间数据交互
首先下载GatewayWorker 框架,官网下载地址:点击下载
解压后获得如下内容:
复制Applications下的YourApp,重命名为自己的应用名称。如:MyApp。
若为windows系统,需要手动替换start_for_win.bat内的所有YourApp为你的应用名。
修改新应用内Events.php中Events类的内容为如下所示:
class Events { /** * 当客户端连接时触发 * 如果业务不需此回调可以删除onConnect * * @param int $client_id 连接id */ public static function onConnect($client_id) { $initMsg = [ 'type' => 'init', 'client_id' => $client_id ]; Gateway::sendToClient($client_id, json_encode($initMsg)); } /** * 当客户端发来消息时触发 * @param int $client_id 连接id * @param mixed $message 具体消息 */ public static function onMessage($client_id, $message) { } }
根据自身状况分别修改start_gateway.php,start_register.php,start_businessworker.php的内容,示例如下:
start_gateway.php,注意`new Gateway`中的协议必须为`websocket`:
// gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:8233"); // gateway名称,status方便查看 $gateway->name = 'MyAppGateway'; // gateway进程数 $gateway->count = 4; // 本机ip,分布式部署时使用内网ip $gateway->lanIp = '127.0.0.1'; // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000 // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口 $gateway->startPort = 2900; // 服务注册地址 $gateway->registerAddress = '127.0.0.1:1233';
start_register.php:
// register 必须是text协议 $register = new Register('text://0.0.0.0:1233');
start_businessworker.php:
// bussinessWorker 进程 $worker = new BusinessWorker(); // worker名称 $worker->name = 'MyAppBusinessWorker'; // bussinessWorker进程数量 $worker->count = 4; // 服务注册地址 $worker->registerAddress = '127.0.0.1:1233';
启动服务:
windows:
双击start_for_win.bat
Linux:
启动:
以debug(调试)方式启动:
php start.php start
以daemon(守护进程)方式启动:
php start.php start -d
停止:
php start.php stop
重启:
php start.php restart
平滑重启:
php start.php reload
查看状态:
php start.php status
查看连接状态:
php start.php connections
示例如图(windows版):
至此WorkerMan服务已经成功启动,接下来对PHP站点进行配置:
首先下载GatewayClient,官方github:点击前往
解压后获得如下内容:
在外层目录新建文件index.php,worker.php:
两个文件内容分别为:
index.php
<?php if(empty($_GET['uid'])) { $_GET['uid'] = 1; } ?> <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> <title>-</title> </head> <body> <script type="text/javascript"> ws = new WebSocket("ws://127.0.0.1:8233"); ws.onmessage = function(e) { var data = JSON.parse(e.data); var type = data.type || ''; switch(type) { case 'init': var uid = '<?php echo $_GET["uid"];?>'; $.get('./worker.php', {type: 'bind', client_id: data.client_id, uid: uid}, function(data){}, 'json'); break; case 'msg': alert(data.msg); break; default: break; } }; </script> </body> </html>
worker.php
<?php require_once './GatewayClient/Gateway.php'; use GatewayClient\Gateway; // 设置GatewayWorker服务的Register服务ip和端口,请根据实际情况改成实际值(ip不能是0.0.0.0) Gateway::$registerAddress = '127.0.0.1:1233'; if(!empty($_GET['type'])) { if($_GET['type'] == 'bind') { bind($_GET['client_id'], $_GET['uid']); } if($_GET['type'] == 'sendtoUid') { sendtoUid($_GET['uid'], $_GET['msg']); } if($_GET['type'] == 'sendtoAll') { sendtoAll($_GET['msg']); } } function bind($client_id, $uid) { Gateway::bindUid($client_id, $uid); } function sendtoUid($uid, $msg) { Gateway::sendToUid($uid, json_encode(['type' => 'msg', 'msg' => $msg])); } function sendtoAll($msg) { Gateway::sendToAll(json_encode(['type' => 'msg', 'msg' => $msg])); }
在浏览器中开启2个窗口:
http://你的域名/index.php
http://你的域名/index.php?uid=25
然后分别访问:
http://你的域名/worker.php?type=sendtoAll&msg=hello%20world
http://你的域名/worker.php?type=sendtoUid&uid=25&msg=hello%20world
查看页面交互效果
以上仅对GatewayWorker配合PHP使用的基本方式做出介绍,更多相关内容请访问官方文档查看。