Skip to content

Commit

Permalink
Support open / close
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Nov 10, 2023
1 parent b7139e5 commit ac2c81f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
57 changes: 30 additions & 27 deletions packages/reactor/resources/servers/swoole_websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ function (OpenEvent $event) use ($app) {
// Keep request in memory, so we can use this request cross processes.
$app->rememberRequest($request);

// Run custom open() code.
$app->openConnection($request);
try {
// Run custom open() code.
$app->openConnection($request);
} catch (\Throwable $e) {
CliServerRuntime::handleThrowable($e);
}
}
);

Expand All @@ -160,8 +164,7 @@ function (OpenEvent $event) use ($app) {
$server->onMessage(
function (MessageEvent $event) use ($app) {
// Get request object from memory
$request = $app->getRequest($event->getFd())
->withFrame($event->getFrame());
$request = $event->getRequestFromMemory($app);

try {
$app->runContextByRequest($request);
Expand All @@ -171,6 +174,29 @@ function (MessageEvent $event) use ($app) {
}
);

/*
* --------------------------------------------------------------------------
* Connection Close
* --------------------------------------------------------------------------
* The event of connection closing.
* This event will release the objects which keeping in memory.
* To add custom close handler, put your code into WsApplication::started()
*/

$server->onClose(
function (CloseEvent $event) use ($app) {
// Get request from memory and release it
$request = $event->getAndForgetRequest($app);

try {
// Run custom close handler.
$app->closeConnection($request);
} catch (\Throwable $e) {
CliServerRuntime::handleThrowable($e);
}
}
);

/*
* --------------------------------------------------------------------------
* Server Start
Expand All @@ -197,29 +223,6 @@ function (StartEvent $event) use ($server, $app) {
}
);

/*
* --------------------------------------------------------------------------
* Connection Close
* --------------------------------------------------------------------------
* The event of connection closing.
* This event will release the objects which keeping in memory.
* To add custom close handler, put your code into WsApplication::started()
*/

$server->onClose(
function (CloseEvent $event) use ($app) {
// Get request from memory
$request = $app->getRequest($event->getFd())
->withFrame($event->createWocketFrame());

// Release request object from memory
$app->forgetRequest($event->getFd());

// Run custom close handler.
$app->closeConnection($request);
}
);

/*
* --------------------------------------------------------------------------
* Boot Application
Expand Down
20 changes: 19 additions & 1 deletion packages/reactor/src/Swoole/Event/CloseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Windwalker\Event\AbstractEvent;
use Windwalker\Reactor\WebSocket\WebSocketFrame;
use Windwalker\Reactor\WebSocket\WebSocketFrameInterface;
use Windwalker\Reactor\WebSocket\WebSocketRequestInterface;
use Windwalker\WebSocket\Application\WsApplicationInterface;

/**
* The CloseEvent class.
Expand All @@ -16,8 +18,24 @@ class CloseEvent extends AbstractEvent
use ServerEventTrait;
use TcpEventTrait;

public function createWocketFrame(): WebSocketFrameInterface
public function createWebSocketFrame(): WebSocketFrameInterface
{
return new WebSocketFrame($this->getFd());
}

public function getRequestFromMemory(WsApplicationInterface $app): WebSocketRequestInterface
{
return $app->getRememberedRequest($this->getFd())
->withFrame($this->createWebSocketFrame())
->withMethod('CLOSE');
}

public function getAndForgetRequest(WsApplicationInterface $app): WebSocketRequestInterface
{
$request = $this->getRequestFromMemory($app);

$app->forgetRequest($request->getFd());

return $request;
}
}
9 changes: 9 additions & 0 deletions packages/reactor/src/Swoole/Event/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Windwalker\Event\AbstractEvent;
use Windwalker\Reactor\WebSocket\WebSocketFrameInterface;
use Windwalker\Reactor\WebSocket\WebSocketRequestInterface;
use Windwalker\WebSocket\Application\WsApplicationInterface;

/**
* The MessageEvent class.
Expand Down Expand Up @@ -42,4 +44,11 @@ public function getData(): string
{
return $this->frame->getData();
}

public function getRequestFromMemory(WsApplicationInterface $app): WebSocketRequestInterface
{
return $app->getRememberedRequest($this->getFd())
->withFrame($this->getFrame())
->withMethod('MESSAGE');
}
}
4 changes: 3 additions & 1 deletion packages/reactor/src/Swoole/SwooleServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,9 @@ function (Request $request, Response $response) {
$port,
'Open',
function (SwooleBaseServer $swooleServer, Request $swooleRequest) use ($server) {
$request = SwooleRequestFactory::createPsrSwooleRequest($swooleRequest);
$request = SwooleRequestFactory::createPsrSwooleRequest($swooleRequest)
->withMethod('OPEN');

$this->emit(
OpenEvent::class,
compact(
Expand Down
7 changes: 7 additions & 0 deletions packages/reactor/src/WebSocket/WebSocketRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class WebSocketRequest extends ServerRequest implements WebSocketRequestInterfac

protected mixed $parsedData = null;

protected array $allowMethods = [
'GET',
'OPEN',
'MESSAGE',
'CLOSE'
];

public static function createFromFrame(WebSocketFrameInterface $frame): static
{
return (new static())->withFrame($frame);
Expand Down

0 comments on commit ac2c81f

Please sign in to comment.