我使用最简版聊天室进行修改,修改start_web.php,增加响应前端ajax功能,响应后依据获取到的id进行数据库读取并反馈结果给前端;很奇怪的问题是:使用守护模式php start.php -d运行后,ajax功能失效,但是如果使用php start.php运行后,ajax功能是正常的;不知道问题出在哪里,请大神帮忙指导下,谢谢!~~~
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Workerman\Worker;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/../../vendor/autoload.php';
// WebServer
$web = new Worker("http://0.0.0.0:55151");//55151--55251
// WebServer进程数量
$web->count = 4;
define('WEBROOT', __DIR__ . DIRECTORY_SEPARATOR . 'Web');
$config = [
'host'=>'127.0.0.1',
'user'=>'username',
'password'=>'password',
'database'=>'db',
'port'=>'3306',
'charset'=>'utf8',
];
$dsn = sprintf("mysql:host=%s;dbname=%s;port=%s;charset=%s",$config['host'],$config['database'],$config['port'],$config['charset']);
try{
$pdo = new PDO($dsn,$config['user'],$config['password']);
}catch (PDOException $e){
die($e->getMessage());
}
$web->onMessage = function (TcpConnection $connection, Request $request) use ($pdo) {
$_GET = $request->get();
$_POST = $request->post();//定义POST
$path = $request->path();
if ($path === '/') {
$connection->send(exec_php_file(WEBROOT.'/index.php'));
return;
}
// 处理getInfo.php请求
elseif ($path === '/getInfo') {
// 获取POST数据中的id
$id = (int)$_POST['id'] ?? null;//检查是否存在id,不存在则赋值null
// $infodata=$_POST['info'] ?? null;
// $myFilter=$_POST['myFilter'] ?? null;//暂时不用
$info=[];
$myFilter=[];
if ($id!=null&&$id!=0) {
$sql = "select * from eq where id=".$id;
$query = $pdo->query($sql);
$rows = $query->fetch();
if(!($rows==0)) {//find record
$info=[
"id"=>$id,
"type"=>$rows['type'],//TCP or websocket
"code"=>$rows['pid'],
"name"=>$rows['eq_name'],
"recode"=>$rows['recode'],//自动回复信息:如success
"val"=>$rows['val'],//转发字段--msg
"forward"=>"",//数据转发--code类似
"vtype"=>0,//数据转发,转发字段使用:asc or hex
"login"=>0,//单点或多点登陆
"rtype"=>0,//自动回复包:asc or hex
"http"=>"",//http_client 转发url地址
"fz_up"=>$rows['fz_up'],//阀值上限
"fz_dw"=>$rows['fz_dw'],//阀值下限
"alarm_bj"=>$rows['alarm_bj'],//复位标记
"remark"=>""//备注
];
$myFilter=[
"is"=> 0,
"type"=>[],
"lengType"=>"",
"length"=>"",
"before"=>"",
"beforeVal"=>"",
"heartVal"=>""
];
}
} else {
$info=[
"type"=>1,//TCP or websocket
"code"=>generate_code(12),
"name"=>"",
"recode"=>"",//自动回复信息:如success
"val"=>"",//转发字段--msg
"forward"=>"",//数据转发--code类似
"vtype"=>0,//数据转发,转发字段使用:asc or hex
"login"=>0,//单点或多点登陆
"rtype"=>0,//自动回复包:asc or hex
"http"=>"",//http_client 转发url地址
"fz_up"=>"",//阀值上限
"fz_dw"=>"",//阀值下限
"remark"=>""//备注
];
$myFilter=[
"is"=> 0,
"type"=>[],
"lengType"=>"",
"length"=>"",
"before"=>"",
"beforeVal"=>"",
"heartVal"=>""
];
$arrnew=[
"status"=>"success",
"info"=>$info,
"myFilter"=>$myFilter
];
// $response = ['status' => 'success', 'info' => $info, 'myFilter' => $myFilter];
}
// 发送响应
$response = ['status' => 'success', 'info' => $info, 'myFilter' => $myFilter];
$connection->send(json_encode($response));
return;
}
elseif ($path === '/add.php') {
$connection->send(exec_php_file(WEBROOT.'/add.php'));
return;
}
else{
}
};
function generate_code( $length) {//$length = 8
// 密码字符集,可任意添加你需要的字符
$chars = 'abdefghjkmnpqrtABCEFGHJKLMNPQRSTUVWXYZ123456789';//!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
//$chars = '0123456789';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
function exec_php_file($file) {
\ob_start();
// Try to include php file.
try {
include $file;
} catch (\Exception $e) {
echo $e;
}
return \ob_get_clean();
}
// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
正常情况:(非守护模式)
AJAX后获取到后端给的数据;
异常情况:(守护模式)
AJAX后无法获取后端给的数据,显示无响应。
正常情况:(非守护模式)
异常情况:(守护模式)
就是使用最简版聊天室进行修改
按照文档数据库初始化放到onWorkerStart里
好的,我试下看看,谢谢!~~~
我在Events.php中public static function onWorkerStart($worker)
{
感谢大神!~~~就是要在start_web.php里面采取$web->onWorkerStart = function(){
Events::$db = new \Workerman\MySQL\Connection(...);
};这种方式对数据库进行读写才可以,否则对AJAX会造成影响,已解决!~~~