alexboo / websocket-handler
websocket handler
dev-master
2018-01-26 04:00 UTC
Requires
- php: >=5.4.0
- cboden/ratchet: dev-master
This package is not auto-updated.
Last update: 2024-10-26 18:59:58 UTC
README
A small library to define a handler for web socket command
Install
You can install it library with composer
composer require alexboo/websocket-handler
Using
Yoy need create Handler factory for create handler for the processing of each command. It must implements HandlerFactoryInterface. In Handler factory you can add ACL before create handler.
Example handler factory
class HandlerFactory implements HandlerFactoryInterface
{
/**
* Get handler for command
* @param Client $client
* @param $command
* @return Handler
*/
public function getHandler(Client $client, $command)
{
$handler = new Handler();
switch ($command) {
case "auth":
$handler->setObject(new AuthService(), "auth");
break;
case "message":
$handler->setClosure(function(){
// do something
});
break;
}
return $handler;
}
/**
* Client open connection
* @param Client $client
* @return mixed
*/
public function open(Client $client){
}
/**
* Client close connection
* @param Client $client
* @return mixed
*/
public function close(Client $client)
{
}
}
Then you can start web socket server
$wsHandler = WebSocketHandler::getInstance(new HandlerFactory());
$ioServer = IoServer::factory(
new HttpServer(new \Ratchet\WebSocket\WsServer($wsHandler)),
8001
);
$ioServer->run();
Parameters that must be passed to the handler defined on the basis ReflectionApi. You can request information about the client specifying the parameters of the "Client" class or query parameters in object implements Alexboo\WebSocketHandler\Interfaces\RequestInterface.
Example
class AuthRequest implements RequestInterface {
public function setData($data){
}
}
class AuthService {
public function auth(Client $client, AuthRequest $request)
{
}
}