2020-12-14 19:03:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
|
|
|
|
|
2020-12-14 19:11:51 +08:00
|
|
|
use Closure;
|
|
|
|
|
use HttpServer\Route\Node;
|
|
|
|
|
use Snowflake\Exception\ComponentException;
|
|
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
2020-12-14 19:03:05 +08:00
|
|
|
/**
|
|
|
|
|
* Class Socket
|
|
|
|
|
* @package Annotation
|
|
|
|
|
*/
|
|
|
|
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const CLOSE = 'CLOSE';
|
|
|
|
|
const MESSAGE = 'MESSAGE';
|
|
|
|
|
const HANDSHAKE = 'HANDSHAKE';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Socket constructor.
|
|
|
|
|
* @param string $event
|
|
|
|
|
* @param string|null $uri
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
public string $event,
|
|
|
|
|
public ?string $uri
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:11:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure|array $closure
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
*/
|
|
|
|
|
public function setHandler(Closure|array $closure): mixed
|
|
|
|
|
{
|
|
|
|
|
$router = Snowflake::app()->getRouter();
|
|
|
|
|
// TODO: Implement setHandler() method.
|
|
|
|
|
|
|
|
|
|
$method = $this->event . '::' . ($this->uri ?? 'event');
|
|
|
|
|
|
|
|
|
|
return $router->addRoute($method, $closure, 'sw::socket');
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 19:03:05 +08:00
|
|
|
}
|