そのため、私は PHP で Ratchet を使用しており、現在、成功した websocket の例をサーバーにアップロードしています。
SSH にアクセスしてから、「php bin/chat-server.php」を手動で実行すると機能します。
私が疑問に思っていたのは、商業的な状況で、チャット サーバーを稼働させ続けるにはどうすればよいかということです。
ありがとう。
デーモンを作ります。
symfony2 を使用している場合は、Process Componentを使用できます。
// in your server start command
$process = new Process('/usr/bin/php bin/chat-server.php');
$process->start();
sleep(1);
if ($process->isRunning()) {
echo "Server started.\n";
} else {
echo $process->getErrorOutput();
}
// in your server stop command
$process = new Process('ps ax | grep bin/chat-server.php');
$process->run();
$output = $process->getOutput();
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
$ar = preg_split('/\s+/', trim($line));
if (in_array('/usr/bin/php', $ar)
and in_array('bin/chat-server.php', $ar)) {
$pid = (int) $ar[0];
posix_kill($pid, SIGKILL);
$stopped = True;
}
}
if ($stopped) {
echo "Server stopped.\n";
} else {
echo "Server not found. Are you sure it's running?\n";
}
ネイティブ PHP を使用している場合でも、心配する必要はありませんpopen
。
// in your server start command
_ = popen('/usr/bin/php bin/chat-server.php', 'r');
echo "Server started.\n";
// in your server stop command
$output = array();
exec('ps ax | grep bin/chat-server.php', &$output);
$lines = preg_split('/\n/', $output);
// kill everything (there can be multiple processes if they are spawned)
$stopped = False;
foreach ($lines as $line) {
$ar = preg_split('/\s+/', trim($line));
if (in_array('/usr/bin/php', $ar)
and in_array('bin/chat-server.php', $ar)) {
$pid = (int) $ar[0];
posix_kill($pid, SIGKILL);
$stopped = True;
}
}
if ($stopped) {
echo "Server stopped.\n";
} else {
echo "Server not found. Are you sure it's running?\n";
}
もちろん、デーモンを操作するための他の便利な PHP ライブラリもあります。「phpデーモン」をグーグルで検索すると、多くのポインタが得られます。
ラチェットのドキュメントには展開ページがあります。チェックしましたか?
古い答え: prod サーバーでは悪い考えかもしれませんが (これは個人的な仮定です)、screen
コマンドを使用してターミナルを開き、デーモンを起動してから、Ctrl-A、Ctrl-D を押すと、ターミナルがまだ生きていて、バックグラウンドで開いています。このターミナルに再接続するには、サーバーに接続し直して と入力しますscreen -r
。
このチュートリアルでは、WebSocket を *nix サービスに変換して、SSH 接続を閉じても維持されるようにする、非常に優れた方法を示します。
基本的には/etc/init/socket.conf
、次の内容のファイルを作成します
# Info
description "Runs the 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/
/etc/rc.d/rc
*nix サーバー用に開始します。これにより、サーバーが起動するたびに PHP スクリプトが起動します。
私は現在、プログラミング/Linuxの愛好家であり学生であるため、業界がそれをどのように行っているかは実際にはわかりませんが、それが個人サーバーで行くルートです。