Last active
August 29, 2024 05:33
-
-
Save cboden/3119135 to your computer and use it in GitHub Desktop.
Ratchet Routing
This file contains hidden or 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 | |
| $collection = new RouteCollection; | |
| $collection->add('chatRoom', new Route('/demo', array( | |
| '_controller' => new ChatRoom | |
| , 'allowedOrigins' => 'socketo.me' | |
| ))); | |
| $collection->add('echo', new Route('/echo', array( | |
| '_controller' => new AbFuzzyServer | |
| , 'allowedOrigins' => '*' | |
| ))); | |
| $collection->add('uhOh', new Route('/what/{happens}/{now}', array( | |
| '_controller' => new Trouble | |
| ))); | |
| $router = new WsRouter; | |
| $router->addCollection($collection); | |
| $wsserv = new WebSocket($router); | |
| $server = new IoServer($wsserv); | |
| $server->run(); |
This file contains hidden or 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\ConnectionInterface; | |
| use Ratchet\MessageComponentInterface; | |
| class Trouble implements MessageComponentInterface { | |
| public function onOpen(ConnectionInterface $conn, , $happens = '', $now = '') { // ??? | |
| $from->send('Welcome to ' . $from->WebSocket->Router->path); | |
| } | |
| public function onMessage(ConnectionInterface $from) { | |
| } | |
| public function onClose(ConnectionInterface $conn) { | |
| } | |
| public function onError(ConnectionInterface $conn, \Exception $e) { | |
| } | |
| } |
@cmancone this is actually the only thing worked for me!
@cmancone Thanks man
Here is a working example for Laravel. My current version is 0.4.2.
https://gist.github.com/rahulhaque/2a92380e54779d03660e01da3851d24d
Does the App class of the ratchet not work anymore in version 0.4.4?
Here is a working example for Laravel. My current version is 0.4.2.
https://gist.github.com/rahulhaque/2a92380e54779d03660e01da3851d24d
Thank you man, searched all internet for a week!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed the routing feature, but this doc is very out-of-date and most of the classes don't exist anymore. I did lots of digging through the code, the tests, and the symfony routing components to finally come up with an example that actually works. I did have to do something weird though, and I don't know if that is because things just don't line up anymore, or because I'm missing something (my example works though).
The issue I ran into is that my first attempts with the Router ran but didn't work - the clients would complain that the connection was never established. After more digging I learned that the actual establishment of the connection happens in the
WsServerclass and isn't handled by theRouter. Perhaps theWsRouterwould provide this functionality, but in my verson (0.4.1) it does not exist, nor does theWebSocketclass that is wrapped around theWsRouter.This meant that I needed the
WsServerclass to get in the mix to properly establish connections (either that or add the connection management to my controller, but that would rather defeat the purpose). This means that the WsServer needed to be passed as the controller of the route. Unfortunately theWsServerclass needs to be given an instance of theMessageComponentInterfaceclass (aka the class with my actual websocket functionality), but the router wants the controller class and will instantiate it itself. This means that theWsServercannot be passed in as a controller to theRouter, because there is no way to specify whichMessageComponentInterfaceimplementer theWsServershould use. To fix this I created a separate class for each of myMessageComponentInterfaceclasses which extends theWsServerand manually instantiates theMessageComponentInterfaceclass and passes it into theWsServerconstructor. This worked.This process was definitely fairly worky, but so far I love the interface/setup this project provides otherwise. Anyway, here's the working example: