我的本地主机上有一个 Ratchet(PHP 实时类)项目。当我想使用和测试这个项目时,
我运行 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(类):
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 根目录中编写此代码
nohup php server.php
关于php - 如何让 Ratchet WebSocket 保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489377/