Events WebSocket
This commit is contained in:
23
common/Alias/Event/Body.php
Normal file
23
common/Alias/Event/Body.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias\Event;
|
||||
|
||||
use ProVM\Common\Define\Event\Body as BodyInterface;
|
||||
|
||||
class Body implements BodyInterface {
|
||||
protected $body = [];
|
||||
public function write($data) {
|
||||
if (is_object($data)) {
|
||||
return $this->write((array) $data);
|
||||
}
|
||||
if (!is_array($data)) {
|
||||
$this->body []= $data;
|
||||
return;
|
||||
}
|
||||
foreach ($data as $key => $line) {
|
||||
$this->body[$key] = $line;
|
||||
}
|
||||
}
|
||||
public function read() {
|
||||
return $this->body;
|
||||
}
|
||||
}
|
46
common/Alias/Event/Message.php
Normal file
46
common/Alias/Event/Message.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias\Event;
|
||||
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface as Dispatcher;
|
||||
use Ratchet\MessageComponentInterface as Component;
|
||||
use Ratchet\ConnectionInterface as Connection;
|
||||
use ProVM\Common\Factory\Event\Request as RequestBuilder;
|
||||
|
||||
class Message implements Component {
|
||||
protected $clients;
|
||||
public function __construct($storage) {
|
||||
$this->clients = $storage;
|
||||
}
|
||||
protected $dispatcher;
|
||||
public function setDispatcher(Dispatcher $dispatcher) {
|
||||
$this->dispatcher = $dispatcher;
|
||||
return $this;
|
||||
}
|
||||
protected $request_builder;
|
||||
public function setRequestBuilder(RequestBuilder $builder) {
|
||||
$this->request_builder = $builder;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onOpen(Connection $conn) {
|
||||
$this->clients->attach($conn);
|
||||
}
|
||||
public function onMessage(Connection $from, $msg) {
|
||||
foreach ($this->clients as $client) {
|
||||
if ($client != $from) {
|
||||
continue;
|
||||
}
|
||||
$request = $this->request_builder->build($msg);
|
||||
$response = $this->dispatcher->dispatch($request);
|
||||
$from->send($response);
|
||||
}
|
||||
}
|
||||
public function onClose(Connection $conn) {
|
||||
$this->clients->detach($conn);
|
||||
}
|
||||
public function onError(Connection $conn, \Exception $e) {
|
||||
$conn->send(json_encode($e));
|
||||
$conn->close();
|
||||
}
|
||||
}
|
35
common/Alias/Event/Request.php
Normal file
35
common/Alias/Event/Request.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias\Event;
|
||||
|
||||
use ProVM\Common\Define\Event\{Request as RequestInterface, Body};
|
||||
|
||||
class Request implements RequestInterface {
|
||||
protected $action;
|
||||
public function setAction(string $action) {
|
||||
$this->action = $action;
|
||||
}
|
||||
protected $method;
|
||||
public function setMethod(string $method) {
|
||||
$this->method = $method;
|
||||
}
|
||||
protected $body;
|
||||
public function setBody(Body $body) {
|
||||
$this->body = $body;
|
||||
}
|
||||
public function getAction(): string {
|
||||
return $this->action;
|
||||
}
|
||||
public function getMethod(): string {
|
||||
return $this->method;
|
||||
}
|
||||
public function getBody(): Body {
|
||||
return $this->body;
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'action' => $this->action,
|
||||
'method' => $this->method,
|
||||
'body' => $this->body->read()
|
||||
];
|
||||
}
|
||||
}
|
27
common/Alias/Event/Response.php
Normal file
27
common/Alias/Event/Response.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias\Event;
|
||||
|
||||
use ProVM\Common\Define\Event\{Response as ResponseInterface, Request, Body};
|
||||
|
||||
class Response implements ResponseInterface {
|
||||
protected $request;
|
||||
public function setRequest(Request $request) {
|
||||
$this->request = $request;
|
||||
}
|
||||
protected $body;
|
||||
public function setBody(Body $body) {
|
||||
$this->body = $body;
|
||||
}
|
||||
public function getRequest(): Request {
|
||||
return $this->request;
|
||||
}
|
||||
public function getBody(): Body {
|
||||
return $this->body;
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'request' => $this->request->jsonSerialize(),
|
||||
'body' => $this->body->read()
|
||||
];
|
||||
}
|
||||
}
|
20
common/Alias/Server/App.php
Normal file
20
common/Alias/Server/App.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias\Server;
|
||||
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use Ratchet\Server\IoServer;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface as Dispatcher;
|
||||
|
||||
class App extends IoServer {
|
||||
protected $container;
|
||||
public function setContainer(Container $container) {
|
||||
$this->container = $container;
|
||||
}
|
||||
public function getContainer(): Container {
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
public function add(string $action, callable $call) {
|
||||
$this->container->get(Dispatcher::class)->register($action, $call);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user