我试图启动棘轮的chat-server.php文件,该文件需要从php文件启动套接字服务器,以便每当客户端访问页面服务器启动,但没有发生。这chat-server.php文件需要从终端运行,但我试图在用户访问时从主页面运行它。如何让脚本在后台运行 - 不工作
文件结构
./index.php
./config.php
./vendor/(all供应商文件)
./src/MyApp /Chat.php
./js/app.js
./inc/connect.php < --database连接文件
./inc/send-message.php
./inc/startServer.php
./inc/bg。 PHP
./inc/serverStatus.txt
./css/index.css
./css/reset.css
./bin/chat-server.php
的config.php
<?php
include_once ("./inc/startServer.php");
?>
聊天server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
if(isset($startNow)){
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
}
?>
bg.php
<?php
$startNow = 1;
include_once ("../bin/chat-server.php");
?>
startServer.php
<?php
$statusFile = dirname(__FILE__).'/serverStatus.txt';
$status = file_get_contents($statusFile);
if($status == "0"){
function execInBackground($cmd) {
if(substr(php_uname(), 0, 7) == "WINDOWS") {
pclose(popen('start /B ' . $cmd, 'r'));
file_put_contents($statusFile, 1);
}
else {
exec($cmd . ' > /dev/null &');
file_put_contents($statusFile, 1);
}
}
execInBackground("php " . dirname(__FILE__) . "/bg.php");
}
?>
serverStatus.txt
它的默认值已被设置为
注意:相应地更新了代码,但现在仍然不起作用。
加我对重新启动服务器以避免任何故障或在所有客户端离开时如何关闭服务器没有任何意见。
我真的不知道为什么这个问题是downvote,但仍然感谢它。 – Ayan
您正在为每个客户启动一个新服务器?为什么你需要手动启动服务器而不是在后台运行?这个问题只是尖叫XY问题。 – PeeHaa
@PeeHaa,不,这个代码应该只为所有的客户端启动一个服务器。看看,有关于服务器状态的检查。 – spirit