gpt4 book ai didi

php - 几个小时后 Ratchet Websocket 服务器停止响应

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:50 24 4
gpt4 key购买 nike

我正在为我的 websocket 应用程序使用 Ratchet 。下面是我的服务器代码。

聊天服务器.php

error_reporting(0);
date_default_timezone_set('Europe/Berlin');

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

$docRoot = "http://myshowcam.com/NewSite";
if (!isset($conn)) {
$host = "127.0.0.1"; // Hostname
$user = "root"; // Username Here
$pass = "xxxxx"; //Password Here
$db = "myshowcam"; // Database Name
$dbh = new PDO('mysql:dbname=' . $db . ';host=' . $host, $user, $pass);
}
$server=IoServer::factory(new HttpServer(new WsServer(new Chat())), 9000, "myshowcam.com");
$server->run();

为了运行服务器,我使用了下面的 nohup 命令

nohup php -q ratchet/bin/chat-server.php > ratchet_ws.log &

由于进程在几个小时后停止工作,我使用了一个 shell 进程来检查该服务器是否正在运行,如果它没有运行,将再次启动服务器。

check_process.sh

while true
do
if pgrep php > /dev/null
then
echo Running
else
cd /mypath/project foldername/
nohup php -q ratchet/bin/chat-server.php > ratchet_ws.log &
fi
sleep 1;
done

现在我运行的不是 chat-server.php,而是如下所示。

nohup sh chk_process.sh > check_process.log &

现在我的 chat-server.php 运行正常,但几个小时后没有响应。

感谢您的帮助。

最佳答案

nohup sh 不是保存 WebSocket PHP 脚本的最佳方式。您实际上可以将 PHP 脚本作为系统服务运行,并使用 service socket startservice socket stop 对其进行控制。

您可以通过创建一个包含以下内容的文件 /etc/init/socket.conf 来完成此操作

# Info
description "Runs the Chat Web Socket"
author "Your Name Here"

# Events
start on startup
stop on shutdown

# Automatically respawn
respawn
respawn limit 20 5

# Run the script!
# Note, in this example, if your PHP script (the socket) returns
# the string "ERROR", the daemon will stop itself.
script
[ $(exec /usr/bin/php -f /path/to/socket.php) = 'ERROR' ] && ( stop; exit 1; )
end script

教程引用:http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/

关于php - 几个小时后 Ratchet Websocket 服务器停止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com