如何让 Ratchet WebSocket 保持活动状态?
How to Keep a Ratchet WebSocket alive?
我的本地主机上有一个 Ratchet(PHP 实时 Class)项目。当我要使用和测试这个项目时,
我 运行 CMD 并使用此命令:C:\wamp\www\ChatApp> php cmd.php
(cmd.php 是 websocket 启动器)。
当我按 Ctrl + C
或退出 cmd 时,这个 websocket 就死了。
但是,当我将项目传输到主要主机以激活此 websocket 时,我能做什么?
cmd.php:
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'Chat.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new \ChatApp\Chat()
)
), 8080
);
$server->run();
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 8080
);
$server->run();
Chat.php (Class):
namespace ChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage();
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo 'Someone Connected'.PHP_EOL;
}
public function onMessage(ConnectionInterface $from,$msg) {
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn){
$this->clients->detach($conn);
echo 'Someone Has Disconnected'.PHP_EOL;
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An Error Has Occurred: {$e->getMessage()}\n";
$conn->close();
}
}
您可以在 linux 中使用 ( nohup ) 命令。
将此代码写入您的 server.php root
nohup php server.php &
要了解有关此命令的更多信息,browse this link
您可以在 linux 中使用 ( nohup ) 命令。将此代码写入您的 server.php root
nohupphpserver.php
我的本地主机上有一个 Ratchet(PHP 实时 Class)项目。当我要使用和测试这个项目时,
我 运行 CMD 并使用此命令:C:\wamp\www\ChatApp> php cmd.php
(cmd.php 是 websocket 启动器)。
当我按 Ctrl + C
或退出 cmd 时,这个 websocket 就死了。
但是,当我将项目传输到主要主机以激活此 websocket 时,我能做什么?
cmd.php:
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require 'Chat.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new \ChatApp\Chat()
)
), 8080
);
$server->run();
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
), 8080
);
$server->run();
Chat.php (Class):
namespace ChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage();
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo 'Someone Connected'.PHP_EOL;
}
public function onMessage(ConnectionInterface $from,$msg) {
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn){
$this->clients->detach($conn);
echo 'Someone Has Disconnected'.PHP_EOL;
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An Error Has Occurred: {$e->getMessage()}\n";
$conn->close();
}
}
您可以在 linux 中使用 ( nohup ) 命令。 将此代码写入您的 server.php root
nohup php server.php &
要了解有关此命令的更多信息,browse this link
您可以在 linux 中使用 ( nohup ) 命令。将此代码写入您的 server.php root
nohupphpserver.php