Multiple Servers on the same App

142 views
Skip to first unread message

vicab...@gmail.com

unread,
Mar 2, 2015, 11:16:41 AM3/2/15
to ratch...@googlegroups.com
Hi everyone,

I'm trying to setup two servers (HTTP and WS) in the same application (different ports) but I'm not successful. I'm wondering what I'm doing wrong or even if that's not possible. When I only run one of them, they do work, but if I try to run both at the same time, none of them works.
This is the code of my servers:
<?php


namespace Wings;

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;


use Wings;


require '../../php-autoloader-1.14.4/autoloader.php';
require(__DIR__.'/../../Ratchet/autoload.php');


$wsServer
= IoServer::factory(
   
new HttpServer(
       
new WsServer(
           
new Wings\WsServer\ConnectionHandler()
       
)
   
),
   
8081
);


echo
'Hola';


$httpServer
= IoServer::factory(
   
new HttpServer(
           
new Wings\HttpServer\ConnectionHandler()
   
),
   
8080
);


$httpServer
->run();


$wsServer
->run();
Thank you all.

cboden

unread,
May 16, 2015, 12:47:05 AM5/16/15
to ratch...@googlegroups.com, vicab...@gmail.com
This is doable. A slight change to your code is to create a React Event Loop and inject it into each of your IOServer's instead of using it's factory:

$loop = React\EventLoop\Factory::create();
$wsServer = new IoServer(new HttpServer(etc), 8001, $loop);
$httpServer = new IoServer(new HttpServer(etc), 8080, $loop);

$loop->run();

IoServer::factory creates a new React Event Loop. Only one Event Loop can be running at a time. 
Reply all
Reply to author
Forward
0 new messages