Last active
August 10, 2022 23:57
-
-
Save sudoanand/72cee02730cd8e3ec1cf92d0df1d5241 to your computer and use it in GitHub Desktop.
Ratchet Wildcard Route usage example, complete guide: https://hack4m.com/how-to-implement-wildcard-routes-in-ratchet-websocket-server
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 | |
//Complete guide: https://hack4m.com/how-to-implement-wildcard-routes-in-ratchet-websocket-server | |
use Ratchet\MessageComponentInterface; | |
use Ratchet\ConnectionInterface; | |
use Ratchet\Server\IoServer; | |
use Ratchet\Http\HttpServer; | |
use Ratchet\WebSocket\WsServer; | |
use Ratchet\Http\HttpServerInterface; | |
use ChatApp\Chat; | |
use Guzzle\Http\Message\RequestInterface; | |
// Make sure composer dependencies have been installed | |
//Might be different path for you | |
require __DIR__ . '/../vendor/autoload.php'; | |
class MyApp implements MessageComponentInterface{ | |
public function __construct() {} | |
public function onOpen(ConnectionInterface $conn) { | |
//The request instance | |
$request = $conn->httpRequest; | |
//convert query string to array and store it | |
parse_str($request->getUri()->getQuery(),$query_strings); | |
//Here is what we are looking for: wildcard route values | |
print_r($query_string); //Prints ["something"=>"Value","anotherThing"=>Value] | |
} | |
public function onMessage(ConnectionInterface $from, $msg) { | |
//Your implementation here | |
} | |
public function onClose(ConnectionInterface $conn) { | |
//Your implementation here | |
} | |
public function onError(ConnectionInterface $conn, \Exception $e) { | |
$conn->close(); | |
} | |
} | |
$server = new Ratchet\App("localhost",12345, '0.0.0.0'); | |
$server->route('/{something}/{anotherThing}', new MyApp(), array('*')); | |
$server->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I want to access WebSocket server with wildcard path, like
/api/apiket123456
/api/apiket123457
/api/apiket123458
/api/apiket123459
Can I write
?
Or, how can I do it?