我使用最簡(jiǎn)版聊天室進(jìn)行修改,修改start_web.php,增加響應(yīng)前端ajax功能,響應(yīng)后依據(jù)獲取到的id進(jìn)行數(shù)據(jù)庫(kù)讀取并反饋結(jié)果給前端;很奇怪的問(wèn)題是:使用守護(hù)模式php start.php -d運(yùn)行后,ajax功能失效,但是如果使用php start.php運(yùn)行后,ajax功能是正常的;不知道問(wèn)題出在哪里,請(qǐng)大神幫忙指導(dǎo)下,謝謝!~~~
<?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.wtbis.cn/
* @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進(jìn)程數(shù)量
$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請(qǐng)求
elseif ($path === '/getInfo') {
// 獲取POST數(shù)據(jù)中的id
$id = (int)$_POST['id'] ?? null;//檢查是否存在id,不存在則賦值null
// $infodata=$_POST['info'] ?? null;
// $myFilter=$_POST['myFilter'] ?? null;//暫時(shí)不用
$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'],//自動(dòng)回復(fù)信息:如success
"val"=>$rows['val'],//轉(zhuǎn)發(fā)字段--msg
"forward"=>"",//數(shù)據(jù)轉(zhuǎn)發(fā)--code類似
"vtype"=>0,//數(shù)據(jù)轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)字段使用:asc or hex
"login"=>0,//單點(diǎn)或多點(diǎn)登陸
"rtype"=>0,//自動(dòng)回復(fù)包:asc or hex
"http"=>"",//http_client 轉(zhuǎn)發(fā)url地址
"fz_up"=>$rows['fz_up'],//閥值上限
"fz_dw"=>$rows['fz_dw'],//閥值下限
"alarm_bj"=>$rows['alarm_bj'],//復(fù)位標(biāo)記
"remark"=>""http://備注
];
$myFilter=[
"is"=> 0,
"type"=>[],
"lengType"=>"",
"length"=>"",
"before"=>"",
"beforeVal"=>"",
"heartVal"=>""
];
}
} else {
$info=[
"type"=>1,//TCP or websocket
"code"=>generate_code(12),
"name"=>"",
"recode"=>"",//自動(dòng)回復(fù)信息:如success
"val"=>"",//轉(zhuǎn)發(fā)字段--msg
"forward"=>"",//數(shù)據(jù)轉(zhuǎn)發(fā)--code類似
"vtype"=>0,//數(shù)據(jù)轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)字段使用:asc or hex
"login"=>0,//單點(diǎn)或多點(diǎn)登陸
"rtype"=>0,//自動(dòng)回復(fù)包:asc or hex
"http"=>"",//http_client 轉(zhuǎn)發(fā)url地址
"fz_up"=>"",//閥值上限
"fz_dw"=>"",//閥值下限
"remark"=>""http://備注
];
$myFilter=[
"is"=> 0,
"type"=>[],
"lengType"=>"",
"length"=>"",
"before"=>"",
"beforeVal"=>"",
"heartVal"=>""
];
$arrnew=[
"status"=>"success",
"info"=>$info,
"myFilter"=>$myFilter
];
// $response = ['status' => 'success', 'info' => $info, 'myFilter' => $myFilter];
}
// 發(fā)送響應(yīng)
$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();
}
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
正常情況:(非守護(hù)模式)
AJAX后獲取到后端給的數(shù)據(jù);
異常情況:(守護(hù)模式)
AJAX后無(wú)法獲取后端給的數(shù)據(jù),顯示無(wú)響應(yīng)。
正常情況:(非守護(hù)模式)
異常情況:(守護(hù)模式)
就是使用最簡(jiǎn)版聊天室進(jìn)行修改
按照文檔數(shù)據(jù)庫(kù)初始化放到onWorkerStart里
我在Events.php中public static function onWorkerStart($worker)
{
self::$db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'user', 'pass', 'db');}使用這種方式進(jìn)行初始化,那我在Start_web.php中調(diào)用是不是就采取$result = Events::$db->query("select * from eq where id=".$id);//這種方式進(jìn)行讀取就可以了,但是實(shí)際運(yùn)行不行,報(bào)錯(cuò)“Error: Call to a member function query() on null in /var/www/html/Applications/Chat/start_web.php:66”,應(yīng)該怎么引用?
$web->onWorkerStart = function(){
Events::$db = new \Workerman\MySQL\Connection(...);
};
感謝大神!~~~就是要在start_web.php里面采取$web->onWorkerStart = function(){
Events::$db = new \Workerman\MySQL\Connection(...);
};這種方式對(duì)數(shù)據(jù)庫(kù)進(jìn)行讀寫才可以,否則對(duì)AJAX會(huì)造成影響,已解決!~~~