2015-07-03 49 views
-1

裝入類 「IO服務器」 我有這樣的命令:的Symfony 2,試圖從命名空間 「瑞奇服務器」

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

該類確實存在。 我做作曲家需要guzzle/guzzle &&作曲家需要cboden /棘輪。 –

回答

0

舉個例子,我將使用8080端口:
1.首先創建一個包,用於管理套接字連接:位於應用程序\ SocketBundle
2.創建一個處理程序(服務):應用程序\ SocketBundle \處理程序

<?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(); 
    } 
} 
  • 在應用/ SocketBundle /資源/配置/註冊的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 /命令 一個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 
    
    相關問題