我有这个命令:
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_main:sock_server_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无法找到这个课程?解决方案是什么?
答案 0 :(得分:0)
例如,我将使用端口8080:
1.首先创建一个用于管理套接字连接的bundle: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();
}
}
在App / SocketBundle / Resources / config / services.xml注册服务
<?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>
4.不要忘记在app / config.yml上加载新资源文件
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "@AppSocketBundle/Resources/config/services.xml" }
5.在App / SocketBundle / Command上创建symfony命令
<?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();
}
}
6.最后,运行命令:
php app/console run:socket