我有一个棘轮(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();
}
}发布于 2020-07-25 09:38:42
您可以在linux中使用( nohup )命令。在server.php根目录中编写这段代码
nohup server.php
发布于 2020-07-25 09:29:53
https://stackoverflow.com/questions/61489377
复制相似问题