Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #54 from swooletw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
albertcht authored May 13, 2018
2 parents 5e2a442 + 106ec24 commit 1307c4e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
6 changes: 3 additions & 3 deletions config/swoole_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
'port' => env('SWOOLE_HTTP_PORT', '1215'),
'public_path' => base_path('public'),
// If use swoole to respond request for static files
'handle_static_files' => true,
// Determine if to use swoole to respond request for static files
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
'options' => [
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
Expand All @@ -35,7 +35,7 @@
'max_request' => 3000,
// Enable coroutine send
'send_yield' => true,
// You must --enable-openssl while compiling Swoole
// You must add --enable-openssl while compiling Swoole
'ssl_cert_file' => null,
'ssl_key_file' => null,
],
Expand Down
4 changes: 2 additions & 2 deletions routes/websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
// called while socket on connect
});

Websocket::on('close', function ($websocket) {
// called while socket on close
Websocket::on('disconnect', function ($websocket) {
// called while socket on disconnect
});

Websocket::on('example', function ($websocket, $data) {
Expand Down
22 changes: 22 additions & 0 deletions src/Websocket/Authenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait Authenticatable
{
protected $userId;

/**
* Login using current user.
*/
Expand Down Expand Up @@ -53,6 +55,26 @@ public function toUserId($userIds)
return $this;
}

/**
* Get current auth user id by sender's fd.
*/
public function getUserId()
{
if (! is_null($this->userId)) {
return $this->userId;
}

$rooms = $this->room->getRooms($this->getSender());

foreach ($rooms as $room) {
if (count($explode = explode(static::USER_PREFIX, $room)) === 2) {
$this->userId = $explode[1];
}
}

return $this->userId;
}

/**
* Check if user object implements AuthenticatableContract.
*/
Expand Down
42 changes: 17 additions & 25 deletions src/Websocket/CanWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,24 @@ public function onOpen(Server $server, $swooleRequest)
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();

try {
$this->websocket->reset(true)->setSender($swooleRequest->fd);
// set currnt request to sandbox
$this->sandbox->setRequest($illuminateRequest);
// enable sandbox
$application = $this->sandbox->getLaravelApp();
$this->sandbox->enable();
// check if socket.io connection established
if (! $this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) {
return;
}
$this->websocket->reset(true)->setSender($swooleRequest->fd);
// trigger 'connect' websocket event
if ($this->websocket->eventExists('connect')) {
$this->callOnConnect($illuminateRequest);
// set sandbox container to websocket pipeline
$this->websocket->setContainer($application);
$this->websocket->call('connect', $illuminateRequest);
}
// disable and recycle sandbox resource
$this->sandbox->disable();
} catch (Exception $e) {
$this->logServerError($e);
}
Expand All @@ -93,12 +102,18 @@ public function onMessage(Server $server, Frame $frame)

$this->websocket->reset(true)->setSender($frame->fd);

// enable sandbox
$application = $this->sandbox->getLaravelApp();
$this->sandbox->enable();

// dispatch message to registered event callback
if ($this->websocket->eventExists($payload['event'])) {
$this->websocket->call($payload['event'], $payload['data']);
} else {
$this->websocketHandler->onMessage($frame);
}
// disable and recycle sandbox resource
$this->sandbox->disable();
} catch (Exception $e) {
$this->logServerError($e);
}
Expand Down Expand Up @@ -279,27 +294,4 @@ protected function normalizePushData(array $data)

return [$opcode, $sender, $fds, $broadcast, $assigned, $event, $message];
}

/**
* Call on connect event callback .
*/
protected function callOnConnect($illuminateRequest)
{
// set currnt request to sandbox
$this->sandbox->setRequest($illuminateRequest);

// get application from sandbox
$application = $this->sandbox->getLaravelApp();

// reset session
if (isset($application['session'])) {
$application['session']->flush();
}

// set sandbox container to websocket pipeline
$this->websocket->setContainer($application);
$this->sandbox->enable();
$this->websocket->call('connect', $illuminateRequest);
$this->sandbox->disable();
}
}
1 change: 1 addition & 0 deletions src/Websocket/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public function reset($force = false)

if ($force) {
$this->sender = null;
$this->userId = null;
}

return $this;
Expand Down
12 changes: 12 additions & 0 deletions tests/Websocket/WebsocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ public function testToUser()
$this->assertTrue(in_array(3, $websocket->getTo()));
}

public function testGetUserId()
{
$room = m::mock(RoomContract::class);
$room->shouldReceive('getRooms')
->with($sender = 1)
->once()
->andReturn(['uid_1']);

$websocket = $this->getWebsocket($room)->setSender($sender);
$this->assertEquals($sender, $websocket->getUserId());
}

public function testReset()
{
$websocket = $this->getWebsocket();
Expand Down

0 comments on commit 1307c4e

Please sign in to comment.