问题描述
我有一个 Symfony 应用程序。并与 Socket.io 聊天。
创建了一个简单的 Symfony 命令来运行套接字服务器。
<?PHP
namespace App\Command;
use App\Websocket\MessageHandler;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class WebSocketServerruncommand extends Command
{
protected static $defaultName = "websocket:server:run";
/**
* @var MessageHandler
*/
private $messageHandler;
public function __construct(string $name = null,MessageHandler $messageHandler)
{
parent::__construct($name);
$this->messageHandler = $messageHandler;
}
protected function execute(InputInterface $input,OutputInterface $output)
{
if (!$this->isAlreadySocketServer()) {
$port = 3001;
$output->writeln("Starting server on port " . $port);
$server = IoServer::factory(
new HttpServer(
new WsServer(
$this->messageHandler
)
),$port
);
$server->run();
return 0;
}
return 200;
}
private function isAlreadySocketServer(): bool
{
$connection = @fsockopen('127.0.0.1','3001');
if (is_resource($connection))
{
echo "Webserver Socket already in Open! :) \n";
return true;
}
else
{
echo "Webserver Socket already in Closed! :( \n";
return false;
}
}
}
我有一个想法,每分钟运行一个 cronjob 命令,所以如果套接字服务器不存在 => 它将重新创建一个。但我认为这不是最好的解决方案。
请给我一些建议,我确信应该有另一种clean
方式来“永远”运行 PHP 网络套接字服务器。
问候
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)