Skip to content

Commit

Permalink
修复握手响应返回后写入连接上下文,导致有时刚握手完 WebSocket 通讯获取连接上下文数据不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jan 3, 2020
1 parent 0974360 commit d11efb2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
20 changes: 1 addition & 19 deletions src/Server/WebSocket/Listener/BeforeHandShake.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,7 @@ public function handle(HandShakeEventParam $e)

// 中间件
$dispatcher = RequestContext::getServerBean('HttpDispatcher');
$response = $dispatcher->dispatch($e->request, $e->response);
if(StatusCode::SWITCHING_PROTOCOLS === $response->getStatusCode())
{
// http 路由解析结果
$routeResult = RequestContext::get('routeResult');
$routeResult->routeItem->callable = null;
$routeResult->callable = null;
ConnectContext::muiltiSet([
'httpRouteResult' => $routeResult,
'uri' => $e->request->getUri(),
]);

$server = $e->getTarget();
$request = $e->request;
$server->trigger('open', [
'server' => &$server,
'request' => &$request,
], $this, OpenEventParam::class);
}
$dispatcher->dispatch($e->request, $e->response);
}

}
20 changes: 20 additions & 0 deletions src/Server/WebSocket/Middleware/HandShakeMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php
namespace Imi\Server\WebSocket\Middleware;

use Imi\ConnectContext;
use Imi\RequestContext;
use Imi\Bean\Annotation\Bean;
use Imi\Util\Http\Consts\StatusCode;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Imi\Server\Event\Param\OpenEventParam;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

Expand All @@ -24,6 +27,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// 未做处理则做默认握手处理
$response = $this->defaultHandShake($request, $response);
}
if(StatusCode::SWITCHING_PROTOCOLS === $response->getStatusCode())
{
// http 路由解析结果
$routeResult = RequestContext::get('routeResult');
$routeResult->routeItem->callable = null;
$routeResult->callable = null;
ConnectContext::muiltiSet([
'httpRouteResult' => $routeResult,
'uri' => $request->getUri(),
]);

$server = RequestContext::get('server');
$server->trigger('open', [
'server' => &$server,
'request' => &$request,
], $this, OpenEventParam::class);
}
return $response;
}

Expand Down

0 comments on commit d11efb2

Please sign in to comment.