PHP code example of xtgxiso / webworker-swoole
1. Go to this page and download the library: Download xtgxiso/webworker-swoole library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xtgxiso / webworker-swoole example snippets
= new WebWorker\App("0.0.0.0",1215);
//进程数
$app->count = 4;
//自动加载目录--会加载目录下的所有php文件
$app->autoload = array();
//应用级中间件--对所有访问启用ip限制访问
$app->AddFunc("/",function() {
if ( $this->request->server['remote_addr'] != '127.0.0.1' ) {
$this->ServerHtml("禁止访问");
return true;//返回ture,中断执行后面的路由或中间件,直接返回给浏览器
}
});
//注册路由hello
$app->HandleFunc("/hello",function() {
$this->ServerHtml("Hello World");
});
//注册路由json
$app->HandleFunc("/json",function() {
//以json格式响应
$this->ServerJson(array("name"=>"WebWorker"));
});
//注册路由input
$app->HandleFunc("/input",function() {
//获取body
$body = $this->request->rawContent();
$this->ServerHtml($body);
});
$config = array();
$config["redis"]["host"] = "127.0.0.1";
$config["redis"]["port"] = 6379;
$config["redis"]["password"] = "123456";
$config["redis"]["db"] = 1;
//是否自动初始化连接
$config["redis"]["load"] = 1;
//是否启用协程redis库
$config["redis"]["coroutine"] = 1;
//记录redis连接池数量
$config["redis"]["coroutine_count"] = 0;
//存放所有的redis连接
$config["redis"]["coroutine_pool"] = new SplQueue();
//注册路由redis
$app->HandleFunc("/redis",function() {
$this->redis->set("xtgxiso",time());
$str = $this->redis->get("xtgxiso");
$this->ServerHtml($str);
});
$config['db']['host'] = "127.0.0.1";
$config['db']['user'] = "root";
$config['db']['password'] = "123456";
$config['db']['database'] = "test";
$config['db']['port'] = 3306;
$config['db']['charset'] = "utf8";
$config["db"]["load"] = 1;
$config["db"]["coroutine"] = 1;
$config["db"]["coroutine_count"] = 0;
$config["db"]["coroutine_pool"] = new SplQueue();
//注册路由mysql
$app->HandleFunc("/mysql",function() {
$res = $this->db->query("select * from test where id =2");
$this->ServerJson($res);
});
$app->on404 = function() {
$this->ServerHtml("我的404");
};
$app->run();
git clone [email protected] :xtgxiso/WebWorker-swoole.git
//需要将代码包含Controller.php';
hp';
php demo.php start
php demo.php start -d
php demo.php reload
php demo.php restart