I've the following code:
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$loop = \React\EventLoop\Factory::create();
$socketServer = new \React\Socket\Server('127.0.0.1:8080', $loop);
$httpServer = new \React\Http\Server(function(\Psr\Http\Message\ServerRequestInterface $request) {
return new \React\Http\Response(200, [
'Content-Type' => 'text/plain'
],
'Hello, World'
);
});
$httpServer->listen($socketServer);
$rrServer = new RRServer(); // Implements MessageComponentInterface
$webSocketServer = new IoServer(
new HttpServer(
new WsServer(
$rrServer
)
),
$socketServer,
$loop
);
$webSocketServer->run();
The code works, but I'm only able to access it using http://localhost:8080, and when I try to connect using WebSocket, the connection is opened, and then it immediately closes. Also, When I create a new socket with different port then I'm able to access both using http:// and ws://
What's wrong with my code? How can I run WebSocket and HTTP Server on the same port?