Created
June 18, 2020 10:01
-
-
Save koswarabilly/b4e0232b050c062a09c02bfed3a2ef44 to your computer and use it in GitHub Desktop.
PHP Ratchet Websocket Basic & Custom Endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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