Php Symfony 2试图加载“类”;IoServer";“来自名称空间”;棘轮\服务器“;
我有这个命令:Php Symfony 2试图加载“类”;IoServer";“来自名称空间”;棘轮\服务器“;,php,symfony,Php,Symfony,我有这个命令: class SockServerCommand extends ContainerAwareCommand implements MessageComponentInterface { //... methods for implementing MessageComponentInterface here /** * {@inheritdoc} */ protected function configure
class SockServerCommand extends ContainerAwareCommand implements MessageComponentInterface
{
//... methods for implementing MessageComponentInterface here
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->clients = new \SplObjectStorage;
$this
->setName('game_main:sock_server_command')
->setDescription('Hello PhpStorm');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// $port = $input->getArgument('port');
$server = \Ratchet\Server\IoServer::factory(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
$this
)
),
7000
);
$server->run();
}
}
如果我运行命令app/console game\u main:sock\u server\u command
,则会出现以下错误:
PHP Fatal error: Class 'Ratchet\Server\IoServer' not found in /var/public_html/symfony.loc/www/src/Game/MainBundle/Command/SockServerCommand.php on line 69
[2015-07-03 17:58:01] php.CRITICAL: Fatal Error: Class 'Ratchet\Server\IoServer' not found {"type":1,"file":"/var/public_html/symfony.loc/www/src/Game/MainBundle/Command/SockServerCommand.php","line":69,"level":-1,"stack":[]}
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "IoServer" from namespace "Ratchet\Server".
Did you forget a "use" statement for another namespace?
game_main:sock_server_command
为什么Symfony找不到这个类?解决方案是什么?作为示例,我将使用端口8080:
1.首先创建一个用于管理套接字连接的捆绑包:App\SocketBundle
2.创建位于以下位置的处理程序(服务):App\SocketBundle\handler
<?php
namespace App\SocketBundle\Handler;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class MainSocketHandler implements MessageComponentInterface
{
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage();
}
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg)
{
foreach ($this->clients as $client) {
if ($from === $client) {
$client->send("OK");
}
}
}
public function onClose(ConnectionInterface $conn)
{
// The connection is closed, remove it, as we can no longer send it messages
$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();
}
}
5.在App/SocketBundle/command处创建symfony命令
这个类存在吗?它位于哪个文件中?您是如何安装库的?该类不存在。我是作曲家需要狂饮/狂饮&&作曲家需要cboden/ratchet。
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="main.socket.service.class">App\SocketBundle\Handler\MainSocketHandler</parameter>
</parameters>
<services>
<service id="main.socket.service" class="%main.socket.service.class%">
</service>
</services>
</container>
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "@AppSocketBundle/Resources/config/services.xml" }
<?php
namespace Lima3\SocketBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Ratchet\Server\IoServer;
class RunSocketCommand extends ContainerAwareCommand
{
protected $route;
public function __construct()
{
parent::__construct();
$this->route = 'cd ' . __DIR__ . '/../../../../;php app/console ';
}
protected function configure()
{
$this
->setName('run:socket')
->setDescription('Run Socket');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$mainSocket = $this->getContainer()->get('main.socket.service');
$server = IoServer::factory(
$mainSocket,
8080
);
$server->run();
}
}
php app/console run:socket