Skip to content

Instantly share code, notes, and snippets.

@koswarabilly
Created June 18, 2020 10:01
Show Gist options
  • Save koswarabilly/b4e0232b050c062a09c02bfed3a2ef44 to your computer and use it in GitHub Desktop.
Save koswarabilly/b4e0232b050c062a09c02bfed3a2ef44 to your computer and use it in GitHub Desktop.
PHP Ratchet Websocket Basic & Custom Endpoint
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat; // Your own class to handle websocket message
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
<?php
use Ratchet\Server\IoServer;
use MyApp\Chat; // Your own class to handle websocket message
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Http\Router;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\UrlMatcher;
require dirname(__DIR__) . '/vendor/autoload.php';
class ChatServer extends WsServer {
public function __construct() {
parent::__construct(new Chat());
}
}
$collection = new RouteCollection();
$collection->add('custom', new Route('/custom', [
'_controller' => ChatServer::class,
]));
$server = IoServer::factory(
new HttpServer(
new Router(
new UrlMatcher(
$collection,
new RequestContext()
)
)
),
8080
);
$server->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment