acabo de probar este codigo
Código PHP:
<?php
namespace ReactSocket;
/*
* copiar esta liberia dendtro de la carpeta
* vendor/react/react/src/React/Socket
*/
class Router
{
private $routes;
public function __construct(array $routes)
{
$this->routes = $routes;
}
public function __invoke($request, $response)
{
foreach ($this->routes as $pattern => $controller) {
if ($this->requestMatchesPattern($request, $pattern)) {
$controller($request, $response);
return;
}
}
$this->handleNotFound($request, $response);
}
protected function requestMatchesPattern($request, $pattern)
{
return $pattern === $request->getPath();
}
protected function handleNotFound($request, $response)
{
$response->writeHead(404, array('Content-Type' => 'text/plain'));
$response->end("We are sorry to inform you that the requested resource does not exist.");
}
}
Código PHP:
require 'vendor/autoload.php';
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);
$router = new ReactSocketRouter(array(
'/' => function ($request, $response) use ($loop) {
$response->writeHead(200, array('Content-Type' => 'text/html'));
$fd = fopen('index.html', 'r');
$template = new ReactStreamStream($fd, $loop);
$template->pipe($response);
},
'/message' => function ($request, $response) {
$response->writeHead(200);
$response->write('hola');
$response->end();
},
'/socket.io/xhr-polling/' => function ($request, $response) {
$response->writeHead(200);
$response->end();
},
));
$http->on('request', $router);
$socket->listen(1337,'0.0.0.0');
$loop->run();
luego de eso la libreria me pide esto
Código:
Unexpected response code: 404
GET http://localhost:1337/socket.io/xhr-polling//1357940610802 404 (Not Found)
al parecer no existe este recurso claro porque no esta en el router
pero no se si ese ruteo debe existir en la libereria nativa de socketio