まず、サービスを作成する必要があります。エンティティマネージャと他の依存関係を注入する場合は、そこに配置します。 SRC/MyAppに/ MyBundle /リソース/設定/ services.ymlで
:
services:
chat:
class: MyApp\MyBundle\Chat
arguments:
- @doctrine.orm.default_entity_manager
およびSrc/MyAppに/ MyBundle/Chat.php中:
class Chat implements MessageComponentInterface {
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $em;
/**
* Constructor
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function __construct($em)
{
$this->em = $em;
}
// onOpen, onMessage, onClose, onError ...
次に、コンソールを作りますコマンドを使用してサーバーを実行します。 SRC/MyAppに/ MyBundle /コマンド/ ServerCommand.phpで
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Ratchet\Server\IoServer;
class ServerCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('chat:server')
->setDescription('Start the Chat server');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$chat = $this->getContainer()->get('chat');
$server = IoServer::factory($chat, 8080);
$server->run();
}
}
今、あなたは、依存性の注入とチャットクラスを持っていて、コンソールコマンドとしてサーバを実行することができます。お役に立てれば!
あなたの答えをありがとう、それは私のために働く。 – Ajouve
これは、平均的なPHPホスティングサービスでこれを実行する方法に関するアイデア?私はそのようなポートを開くだけでなく、プロセスを長時間実行することもできないと考えています(おそらくcrontabのハックを使用していますか?) – Jens
私はあなたのポートを使用できないことについて知らないホスティングサービス。あなたのプロバイダに話す必要があるようなものです!この回答を見ることができますhttp://stackoverflow.com/questions/17696344/starting-a-websockets-server-in-php-on-shared-hosting/17705521#17705521としてサーバーを実行する方法の例PHPを使ったデーモン。しかし、symfonyを使用している場合、私はpopenとpassthruではなくProcessコンポーネントを使用します。質問したい場合は、私が使用するコードを表示することができます。 – mattexx