dqm83011 2015-05-28 04:56
浏览 123

Ratchet Websocket服务器在一小时后停止响应

I am using ratchet for my websocket application.Below is my server code.

chat-server.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();

To run the server i have used the nohup command as beolow

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

As the process stop working after few hour i have use a shell process to check if this server is running or not and if its not running will start the server again.

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

Now instead of running the chat-server.php i am running this procss as below.

nohup sh chk_process.sh > check_process.log &

Now my chat-server.php running fine but its not responding after some hour.

Thank you for any help.

  • 写回答

1条回答 默认 最新

  • duan051347 2015-06-15 07:51
    关注

    nohup sh is not the best way to persist a WebSocket PHP script. You can actually run the PHP script as a system service and control it with service socket start and service socket stop.

    You do this by making a file /etc/init/socket.conf with the following contents

    # 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  
    

    Tutorial Reference: http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行