问题描述
我有一个使用Symfony 3.4的Web应用程序,并用Ratchet创建了一个Websocket。
我在一个tuto中看到,好的做法是在命令中初始化websocket。我将实体管理发送到Chat,这是一个MessageComponentInterface。
我的问题是8小时后,entitymanager断开了与数据库的连接。这是消息:Warning: PDOStatement::execute(): MysqL server has gone away Connection 2039 has disconnected
我知道这很正常,因为这是默认超时时间。
所以这是我的问题:我的config / archi很好吗?如何避免这种连接丢失,是否应该为数据库连接设置非常高的超时值? (我认为这是一个糟糕的主意)。 告诉我你的智慧:)
这是我的命令:
class SocketCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
// the name of the command (the part after "bin/console")
->setName('sockets:start-chat')
// the short description shown while running "PHP bin/console list"
->setDescription('Starts the chat socket')
// the full command description shown when running the command with
// the "--help" option
->setHelp('Starts the chat socket');
}
protected function execute(InputInterface $input,OutputInterface $output)
{
$output->writeln([
'Chat socket','============','Starting chat,open your browser.',]);
$app = new App($this->getContainer()->getParameter('base_domain'),8081,'0.0.0.0');
$app->route('/',new Chat($this->getContainer()->get('doctrine')->getManager()));
$output->writeln('ready');
$app->run();
}
}
我的聊天室:
class Chat implements MessageComponentInterface
{
protected $clients;
protected $entityManager;
public function __construct(EntityManager $entityManager) {
$this->clients = new \SplObjectStorage;
$this->entityManager = $entityManager;
}
public function onopen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from,$msg) {
// code
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn,\Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)