Yii2结合Workerman的websocket示例详解
程序员文章站
2022-05-26 13:45:01
前言
本文主要给大家介绍了关于yii2结合workerman的websocket的相关内容,两者都是好东西,我就想着能不能结合起来,这样yii2出现瓶颈的时候有些业务就可...
前言
本文主要给大家介绍了关于yii2结合workerman的websocket的相关内容,两者都是好东西,我就想着能不能结合起来,这样yii2出现瓶颈的时候有些业务就可以平滑地迁移到workerman中。下面话不多说了,来随着小编来一起看看详细的介绍吧
步骤如下
1、安装workerman
composer require workerman/workerman
2、启动workerman
创建commands/workermanwebsocketcontroller.php文件
创建actionindex()函数,用来启动,代码如下
public function actionindex() { if ('start' == $this->send) { try { $this->start($this->daemon); } catch (\exception $e) { $this->stderr($e->getmessage() . "\n", console::fg_red); } } else if ('stop' == $this->send) { $this->stop(); } else if ('restart' == $this->send) { $this->restart(); } else if ('reload' == $this->send) { $this->reload(); } else if ('status' == $this->send) { $this->status(); } else if ('connections' == $this->send) { $this->connections(); } }
添加初始化模块
public function initworker() { $ip = isset($this->config['ip']) ? $this->config['ip'] : $this->ip; $port = isset($this->config['port']) ? $this->config['port'] : $this->port; $wsworker = new worker("websocket://{$ip}:{$port}"); // 4 processes $wsworker->count = 4; // emitted when new connection come $wsworker->onconnect = function ($connection) { echo "new connection\n"; }; // emitted when data received $wsworker->onmessage = function ($connection, $data) { // send hello $data $connection->send('hello ' . $data); }; // emitted when connection closed $wsworker->onclose = function ($connection) { echo "connection closed\n"; }; }
添加启动模块
/** * workman websocket start */ public function start() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'start'; if ($this->daemon) { $argv[2] = '-d'; } // run worker worker::runall(); }
添加停止模块
/** * workman websocket stop */ public function stop() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'stop'; if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); }
添加重启模块
/** * workman websocket restart */ public function restart() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'restart'; if ($this->daemon) { $argv[2] = '-d'; } if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); }
添加重载模块
/** * workman websocket reload */ public function reload() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'reload'; if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); }
添加状态模块
/** * workman websocket status */ public function status() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'status'; if ($this->daemon) { $argv[2] = '-d'; } // run worker worker::runall(); }
添加链接数模块
/** * workman websocket connections */ public function connections() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'connections'; // run worker worker::runall(); }
3、前端调用
<script> // create websocket connection. const ws = new websocket('ws://{{ app.request.hostname }}:2347/'); // 这里是获取的网站的域名,测试的时候可以改为自己的本地的ip地址 // connection opened ws.addeventlistener('open', function (event) { ws.send('hello server!'); }); // listen for messages ws.addeventlistener('message', function (event) { console.log('message from server ', event.data); }); settimeout(function() { ws.send('ssssss'); }, 10000); </script>
4、config参数配置
修改console.php并添加如下代码
'controllermap' => [ 'workerman-web-socket' => [ 'class' => 'app\commands\workermanwebsocketcontroller', 'config' => [ 'ip' => '127.0.0.1', 'port' => '2346', 'daemonize' => true, ], ], ],
5、nginx配置
为什么会用 nginx, 我们正常部署上线是不可能直接使用ip的,这个户存在安全隐患,最好是绑定一个域名
server { charset utf-8; client_max_body_size 128m; listen 2347; server_name www.gowhich.com; # 这里改为自己的域名 access_log /xxx.workerman.access.log; # 换成自己服务器的nginx日志路径 error_log /xxx.workerman.error.log; # 换成自己服务器的nginx日志路径 location / { proxy_pass http://127.0.0.1:2346; # 代理2346 也可以根据项目配置为自己的端口 proxy_set_header x-real-ip $remote_addr; proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection "upgrade"; } }
重新nginx
nginx -s relad 或者 sudo nginx -s reload
然后将第3步的代码加入自己做的视图中,如果没有问题的话,websocket启动后就能正常通讯了。
6、启动workerman websocket
// 启动 ./yii workerman-web-socket -s start -d
如果没有问题的话会得到类似如下的结果
$ ./yii workerman-web-socket -s start -d workerman[workerman-web-socket] start in daemon mode ----------------------- workerman ----------------------------- workerman version:3.5.13 php version:7.1.16 ------------------------ workers ------------------------------- user worker listen processes status durban none websocket://127.0.0.1:2346 4 [ok] ---------------------------------------------------------------- input "php workerman-web-socket stop" to stop. start success.
7、其他
commands/workermanwebsocketcontroller.php 完整代码如下
<?php /** * workmanwebsocket 服务相关 */ namespace app\commands; use workerman\worker; use yii\console\controller; use yii\helpers\console; /** * * workermanwebsocket * * @author durban.zhang <durban.zhang@gmail.com> */ class workermanwebsocketcontroller extends controller { public $send; public $daemon; public $gracefully; // 这里不需要设置,会读取配置文件中的配置 public $config = []; private $ip = '127.0.0.1'; private $port = '2346'; public function options($actionid) { return ['send', 'daemon', 'gracefully']; } public function optionaliases() { return [ 's' => 'send', 'd' => 'daemon', 'g' => 'gracefully', ]; } public function actionindex() { if ('start' == $this->send) { try { $this->start($this->daemon); } catch (\exception $e) { $this->stderr($e->getmessage() . "\n", console::fg_red); } } else if ('stop' == $this->send) { $this->stop(); } else if ('restart' == $this->send) { $this->restart(); } else if ('reload' == $this->send) { $this->reload(); } else if ('status' == $this->send) { $this->status(); } else if ('connections' == $this->send) { $this->connections(); } } public function initworker() { $ip = isset($this->config['ip']) ? $this->config['ip'] : $this->ip; $port = isset($this->config['port']) ? $this->config['port'] : $this->port; $wsworker = new worker("websocket://{$ip}:{$port}"); // 4 processes $wsworker->count = 4; // emitted when new connection come $wsworker->onconnect = function ($connection) { echo "new connection\n"; }; // emitted when data received $wsworker->onmessage = function ($connection, $data) { // send hello $data $connection->send('dddd hello ' . $data); }; // emitted when connection closed $wsworker->onclose = function ($connection) { echo "connection closed\n"; }; } /** * workman websocket start */ public function start() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'start'; if ($this->daemon) { $argv[2] = '-d'; } // run worker worker::runall(); } /** * workman websocket restart */ public function restart() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'restart'; if ($this->daemon) { $argv[2] = '-d'; } if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); } /** * workman websocket stop */ public function stop() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'stop'; if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); } /** * workman websocket reload */ public function reload() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'reload'; if ($this->gracefully) { $argv[2] = '-g'; } // run worker worker::runall(); } /** * workman websocket status */ public function status() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'status'; if ($this->daemon) { $argv[2] = '-d'; } // run worker worker::runall(); } /** * workman websocket connections */ public function connections() { $this->initworker(); // 重置参数以匹配worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'connections'; // run worker worker::runall(); } }
workerman websocket支持的其他命令
重启
$ ./yii workerman-web-socket -s restart -d workerman[workerman-web-socket] restart workerman[workerman-web-socket] is stopping ... workerman[workerman-web-socket] stop success ----------------------- workerman ----------------------------- workerman version:3.5.13 php version:7.1.16 ------------------------ workers ------------------------------- user worker listen processes status durban none websocket://127.0.0.1:2346 4 [ok] ---------------------------------------------------------------- input "php workerman-web-socket stop" to stop. start success.
重载
$ ./yii workerman-web-socket -s reload workerman[workerman-web-socket] reload
状态
$ ./yii workerman-web-socket -s status -g workerman[workerman-web-socket] status ----------------------------------------------global status---------------------------------------------------- workerman version:3.5.13 php version:7.1.16 start time:2018-09-10 11:22:15 run 0 days 0 hours load average: 1.79, 2, 2 event-loop:\workerman\events\swoole 1 workers 4 processes worker_name exit_status exit_count none 0 12 ----------------------------------------------process status--------------------------------------------------- pid memory listening worker_name connections send_fail timers total_request qps status 8283 4m websocket://127.0.0.1:2346 none 0 0 0 0 0 [idle] 8284 4m websocket://127.0.0.1:2346 none 0 0 0 0 0 [idle] 8285 4m websocket://127.0.0.1:2346 none 0 0 0 0 0 [idle] 8286 4m websocket://127.0.0.1:2346 none 0 0 0 0 0 [idle] ----------------------------------------------process status--------------------------------------------------- summary 16m - - 0 0 0 0 0 [summary]
连接数
./yii workerman-web-socket -s connections workerman[workerman-web-socket] connections --------------------------------------------------------------------- workerman connection status -------------------------------------------------------------------------------- pid worker cid trans protocol ipv4 ipv6 recv-q send-q bytes-r bytes-w status local address foreign address
我这里暂时连接的,所以没有连接的信息
停止
$ ./yii workerman-web-socket -s stop workerman[workerman-web-socket] stop workerman[workerman-web-socket] is stopping ... workerman[workerman-web-socket] stop success
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。