Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add onHandShake to InteractsWithWebsocket
  • Loading branch information
killtw committed Jun 11, 2019
commit 8b71ff08df5abe22f4391f0f785aab0983ecf6c6
44 changes: 43 additions & 1 deletion src/Concerns/InteractsWithWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,46 @@ public function onOpen($server, $swooleRequest)
}
}

/**
* @param \Swoole\Http\Request $swooleRequest
* @param \Swoole\Http\Response $response
*
* @return bool
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function onHandShake($swooleRequest, $response)
{
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
$websocket = $this->app->make(Websocket::class);
$sandbox = $this->app->make(Sandbox::class);
$handler = $this->container->make('config')->get('swoole_websocket.handshake.handler');

try {
$websocket->reset(true)->setSender($swooleRequest->fd);
// set currnt request to sandbox
$sandbox->setRequest($illuminateRequest);
// enable sandbox
$sandbox->enable();

if (! $this->app->make($handler)->handle($swooleRequest, $response)) {
return false;
}
// trigger 'connect' websocket event
if ($websocket->eventExists('connect')) {
// set sandbox container to websocket pipeline
$websocket->setContainer($sandbox->getApplication());
$websocket->call('connect', $illuminateRequest);
}

return true;
} catch (Throwable $e) {
$this->logServerError($e);
} finally {
// disable and recycle sandbox resource
$sandbox->disable();
}
}

/**
* "onMessage" listener.
*
Expand Down Expand Up @@ -222,7 +262,9 @@ protected function prepareWebsocket()
$parser = $config->get('swoole_websocket.parser');

if ($isWebsocket) {
$this->events = array_merge($this->events ?? [], $this->wsEvents);
$handshake = $config->get('swoole_websocket.handshake.enabled');

$this->events = array_merge($this->events ?? [], array_merge($this->wsEvents, $handshake ? ['handshake'] : []));
$this->isServerWebsocket = true;
$this->prepareWebsocketRoom();
$this->setPayloadParser(new $parser);
Expand Down