mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
Support WebSocket Context.
Signed-off-by: reasno <guxi99@gmail.com>
This commit is contained in:
parent
112e27157c
commit
e5a94460b9
88
src/websocket-server/src/Context.php
Normal file
88
src/websocket-server/src/Context.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\WebSocketServer;
|
||||
|
||||
use Hyperf\Utils\Context as CoContext;
|
||||
|
||||
class Context
|
||||
{
|
||||
const FD = 'fd';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $container = [];
|
||||
|
||||
public static function set(string $id, $value)
|
||||
{
|
||||
$fd = CoContext::get(Context::FD, 0);
|
||||
$key = sprintf('%s.%s', $fd, $id);
|
||||
data_set(self::$container, $key, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function get(string $id, $default = null, $fd = null)
|
||||
{
|
||||
$fd = $fd ?? CoContext::get(Context::FD);
|
||||
$key = sprintf('%s.%s', $fd, $id);
|
||||
return data_get(self::$container, $key, $default);
|
||||
}
|
||||
|
||||
public static function has(string $id, $fd = null)
|
||||
{
|
||||
$fd = $fd ?? CoContext::get(Context::FD);
|
||||
return isset(self::$container[strval($fd)]) && isset(self::$container[strval($fd)][$id]);
|
||||
}
|
||||
|
||||
public static function destroy(string $id)
|
||||
{
|
||||
$fd = CoContext::get(Context::FD, 0);
|
||||
unset(self::$container[strval($fd)][$id]);
|
||||
}
|
||||
|
||||
public static function release(int $fd)
|
||||
{
|
||||
unset(self::$container[strval($fd)]);
|
||||
}
|
||||
|
||||
public static function copy(int $fromFd, array $keys = []): void
|
||||
{
|
||||
$fd = CoContext::get(Context::FD, 0);
|
||||
$from = self::$container[$fromFd];
|
||||
self::$container[$fd] = ($keys ? array_fill_keys($keys, $from) : $from);
|
||||
}
|
||||
|
||||
public static function override(string $id, \Closure $closure)
|
||||
{
|
||||
$value = null;
|
||||
if (self::has($id)) {
|
||||
$value = self::get($id);
|
||||
}
|
||||
$value = $closure($value);
|
||||
self::set($id, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function getOrSet(string $id, $value)
|
||||
{
|
||||
if (! self::has($id)) {
|
||||
return self::set($id, value($value));
|
||||
}
|
||||
return self::get($id);
|
||||
}
|
||||
|
||||
public static function getContainer()
|
||||
{
|
||||
return self::$container;
|
||||
}
|
||||
}
|
@ -30,6 +30,7 @@ use Hyperf\Utils\Context;
|
||||
use Hyperf\Utils\Coordinator\Constants;
|
||||
use Hyperf\Utils\Coordinator\CoordinatorManager;
|
||||
use Hyperf\WebSocketServer\Collector\FdCollector;
|
||||
use Hyperf\WebSocketServer\Context as WsContext;
|
||||
use Hyperf\WebSocketServer\Exception\Handler\WebSocketExceptionHandler;
|
||||
use Hyperf\WebSocketServer\Exception\WebSocketHandeShakeException;
|
||||
use Psr\Container\ContainerInterface;
|
||||
@ -117,7 +118,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
{
|
||||
try {
|
||||
CoordinatorManager::until(Constants::WORKER_START)->yield();
|
||||
|
||||
Context::set(WsContext::FD, $request->fd);
|
||||
$security = $this->container->get(Security::class);
|
||||
|
||||
$psr7Request = $this->initRequest($request);
|
||||
@ -167,6 +168,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
|
||||
public function onMessage(WebSocketServer $server, Frame $frame): void
|
||||
{
|
||||
Context::set(WsContext::FD, $frame->fd);
|
||||
$fdObj = FdCollector::get($frame->fd);
|
||||
if (! $fdObj) {
|
||||
$this->logger->warning(sprintf('WebSocket: fd[%d] does not exist.', $frame->fd));
|
||||
@ -187,6 +189,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
{
|
||||
$this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd));
|
||||
|
||||
Context::set(WsContext::FD, $fd);
|
||||
$fdObj = FdCollector::get($fd);
|
||||
if (! $fdObj) {
|
||||
return;
|
||||
@ -197,6 +200,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
}
|
||||
|
||||
FdCollector::del($fd);
|
||||
WsContext::release($fd);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -205,6 +209,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
protected function initRequest(SwooleRequest $request): RequestInterface
|
||||
{
|
||||
Context::set(ServerRequestInterface::class, $psr7Request = Psr7Request::loadFromSwooleRequest($request));
|
||||
WsContext::set(ServerRequestInterface::class, $psr7Request);
|
||||
return $psr7Request;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user