中间件
开发者可以开发中间件类,对整个WebSocket请求和响应过程进行自定义处理。
定义中间件
<?php
namespace Imi\Server\WebSocket\Middleware;
use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Server\WebSocket\Message\IFrame;
use Imi\Server\WebSocket\MessageHandler;
use Imi\Server\WebSocket\IMessageHandler;
/**
* @Bean
*/
class RouteMiddleware implements IMiddleware
{
/**
* 处理方法
*
* @param IFrame $frame
* @param IMessageHandler $handler
* @return void
*/
public function process(IFrame $frame, IMessageHandler $handler)
{
}
}
全局中间件
return [
'beans' => [
// 中间件
'WebSocketDispatcher' => [
'middlewares' => [
// 中间件
\Imi\Server\WebSocket\Middleware\RouteMiddleware::class,
],
],
],
];
局部中间件
注解使用
<?php
namespace ImiDemo\WebSocketDemo\MainServer\WSController\Index;
use Imi\ConnectContext;
use Imi\Controller\WebSocketController;
use Imi\Server\Route\Annotation\WebSocket\WSRoute;
use Imi\Server\Route\Annotation\WebSocket\WSAction;
use Imi\Server\Route\Annotation\WebSocket\WSController;
use Imi\Server\Route\Annotation\WebSocket\WSMiddleware;
/**
* 数据收发测试
* @WSController
*/
class Test extends WebSocketController
{
/**
* test
*
* @WSAction
* @WSRoute({"action"="login"})
* @WSMiddleware(XXX::class)
* @WSMiddleware({XXX::class,XXX2::class})
* @return void
*/
public function test($data)
{
}
}
如上代码,同时支持设置单个和多个中间件