workerman和thinkphp完美结合使用源码
程序员文章站
2024-01-11 10:02:40
...
逛thinkphp论坛,看到很多thinkphper在问如何将workerman集成到thinkphp中, 现在特意奉上源码,经过本人自测可以运行
下载workerman, 放入thinkphp,注意与Home平级,就把workerman当做是一个模块。
进入Home/Controller目录,新建WorkermanController.class.php
直接上源码,这里我是以守护进程方式运行的,调试的话,可以去掉daemonize = true 这行.
namespace Admin\Controller;
use Workerman\Worker;
/**
* 用户信息查询
*/
class WorkermanController{
/**
* 用户信息查询
*/
public function index(){
if(!IS_CLI){
die("access illegal");
}
require_once APP_PATH.'Workerman/Autoloader.php';
// 每个进程最多执行1000个请求
define('MAX_REQUEST', 1000);
Worker::$daemonize = true;//以守护进程运行
Worker::$pidFile = '/data/wwwlogs/CMSWorker/workerman.pid';//方便监控WorkerMan进程状态
Worker::$stdoutFile = '/data/wwwlogs/CMSWorker/stdout.log';//输出日志, 如echo,var_dump等
Worker::$logFile = '/data/wwwlogs/CMSWorker/workerman.log';//workerman自身相关的日志,包括启动、停止等,不包含任何业务日志
$worker = new Worker('text://172.16.0.10:10024');
$worker->name = 'CMSWorker';
$worker->count = 2;
//$worker->transport = 'udp';// 使用udp协议,默认TCP
$worker->onWorkerStart = function($worker){
echo "Worker starting...\n";
};
$worker->onMessage = function($connection, $data){
static $request_count = 0;// 已经处理请求数
var_dump($data);
$connection->send("hello");
/*
* 退出当前进程,主进程会立刻重新启动一个全新进程补充上来,从而完成进程重启
*/
if(++$request_count >= MAX_REQUEST){// 如果请求数达到1000
Worker::stopAll();
}
};
$worker->onBufferFull = function($connection){
echo "bufferFull and do not send again\n";
};
$worker->onBufferDrain = function($connection){
echo "buffer drain and continue send\n";
};
$worker->onWorkerStop = function($worker){
echo "Worker stopping...\n";
};
$worker->onError = function($connection, $code, $msg){
echo "error $code $msg\n";
};
// 运行worker
Worker::runAll();
}
}
修改Workerman/Worker.php源码,找到parseCommand()方法,workerman版本3.3.2的话,在586行,修改命令行检测语法:
protected static function parseCommand()
{
global $argv;
// Check argv;
$start_file = $argv[0];
if (!isset($argv[2])) {
exit("Usage: php yourfile.php Controller/Action {start|stop|restart|reload|status|kill}\n");
}
// Get command.
$command = trim($argv[2]);
$command2 = isset($argv[3]) ? $argv[3] : '';
.....
}
OK,此时大功告成。
Linux命令行下运行,注意,此处要切换到thinkphp根目录下面
/usr/local/php/bin/php index.php Workerman/index start
查看运行状态:
/usr/local/php/bin/php index.php Workerman/index status
此处命令行可以放入全局变量中,直接以php运行
thinkphp网站对于代码编辑支持的不好,至少我没找到,今天刚敲出来的代码,本人自测通过~
您也可以参考我的个人网站 http://www.isbala.com/article/269.html
下载workerman, 放入thinkphp,注意与Home平级,就把workerman当做是一个模块。
进入Home/Controller目录,新建WorkermanController.class.php
直接上源码,这里我是以守护进程方式运行的,调试的话,可以去掉daemonize = true 这行.
namespace Admin\Controller;
use Workerman\Worker;
/**
* 用户信息查询
*/
class WorkermanController{
/**
* 用户信息查询
*/
public function index(){
if(!IS_CLI){
die("access illegal");
}
require_once APP_PATH.'Workerman/Autoloader.php';
// 每个进程最多执行1000个请求
define('MAX_REQUEST', 1000);
Worker::$daemonize = true;//以守护进程运行
Worker::$pidFile = '/data/wwwlogs/CMSWorker/workerman.pid';//方便监控WorkerMan进程状态
Worker::$stdoutFile = '/data/wwwlogs/CMSWorker/stdout.log';//输出日志, 如echo,var_dump等
Worker::$logFile = '/data/wwwlogs/CMSWorker/workerman.log';//workerman自身相关的日志,包括启动、停止等,不包含任何业务日志
$worker = new Worker('text://172.16.0.10:10024');
$worker->name = 'CMSWorker';
$worker->count = 2;
//$worker->transport = 'udp';// 使用udp协议,默认TCP
$worker->onWorkerStart = function($worker){
echo "Worker starting...\n";
};
$worker->onMessage = function($connection, $data){
static $request_count = 0;// 已经处理请求数
var_dump($data);
$connection->send("hello");
/*
* 退出当前进程,主进程会立刻重新启动一个全新进程补充上来,从而完成进程重启
*/
if(++$request_count >= MAX_REQUEST){// 如果请求数达到1000
Worker::stopAll();
}
};
$worker->onBufferFull = function($connection){
echo "bufferFull and do not send again\n";
};
$worker->onBufferDrain = function($connection){
echo "buffer drain and continue send\n";
};
$worker->onWorkerStop = function($worker){
echo "Worker stopping...\n";
};
$worker->onError = function($connection, $code, $msg){
echo "error $code $msg\n";
};
// 运行worker
Worker::runAll();
}
}
修改Workerman/Worker.php源码,找到parseCommand()方法,workerman版本3.3.2的话,在586行,修改命令行检测语法:
protected static function parseCommand()
{
global $argv;
// Check argv;
$start_file = $argv[0];
if (!isset($argv[2])) {
exit("Usage: php yourfile.php Controller/Action {start|stop|restart|reload|status|kill}\n");
}
// Get command.
$command = trim($argv[2]);
$command2 = isset($argv[3]) ? $argv[3] : '';
.....
}
OK,此时大功告成。
Linux命令行下运行,注意,此处要切换到thinkphp根目录下面
/usr/local/php/bin/php index.php Workerman/index start
查看运行状态:
/usr/local/php/bin/php index.php Workerman/index status
此处命令行可以放入全局变量中,直接以php运行
thinkphp网站对于代码编辑支持的不好,至少我没找到,今天刚敲出来的代码,本人自测通过~
您也可以参考我的个人网站 http://www.isbala.com/article/269.html
AD:真正免费,域名+虚机+企业邮箱=0元
下一篇: JDK,JRE,JVM三者关系