Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 1307c4e

Browse files
authored
Merge pull request #54 from swooletw/develop
Develop
2 parents 5e2a442 + 106ec24 commit 1307c4e

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

config/swoole_http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
1616
'port' => env('SWOOLE_HTTP_PORT', '1215'),
1717
'public_path' => base_path('public'),
18-
// If use swoole to respond request for static files
19-
'handle_static_files' => true,
18+
// Determine if to use swoole to respond request for static files
19+
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
2020
'options' => [
2121
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
2222
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
@@ -35,7 +35,7 @@
3535
'max_request' => 3000,
3636
// Enable coroutine send
3737
'send_yield' => true,
38-
// You must --enable-openssl while compiling Swoole
38+
// You must add --enable-openssl while compiling Swoole
3939
'ssl_cert_file' => null,
4040
'ssl_key_file' => null,
4141
],

routes/websocket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
// called while socket on connect
1717
});
1818

19-
Websocket::on('close', function ($websocket) {
20-
// called while socket on close
19+
Websocket::on('disconnect', function ($websocket) {
20+
// called while socket on disconnect
2121
});
2222

2323
Websocket::on('example', function ($websocket, $data) {

src/Websocket/Authenticatable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
trait Authenticatable
99
{
10+
protected $userId;
11+
1012
/**
1113
* Login using current user.
1214
*/
@@ -53,6 +55,26 @@ public function toUserId($userIds)
5355
return $this;
5456
}
5557

58+
/**
59+
* Get current auth user id by sender's fd.
60+
*/
61+
public function getUserId()
62+
{
63+
if (! is_null($this->userId)) {
64+
return $this->userId;
65+
}
66+
67+
$rooms = $this->room->getRooms($this->getSender());
68+
69+
foreach ($rooms as $room) {
70+
if (count($explode = explode(static::USER_PREFIX, $room)) === 2) {
71+
$this->userId = $explode[1];
72+
}
73+
}
74+
75+
return $this->userId;
76+
}
77+
5678
/**
5779
* Check if user object implements AuthenticatableContract.
5880
*/

src/Websocket/CanWebsocket.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,24 @@ public function onOpen(Server $server, $swooleRequest)
5858
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
5959

6060
try {
61+
$this->websocket->reset(true)->setSender($swooleRequest->fd);
62+
// set currnt request to sandbox
63+
$this->sandbox->setRequest($illuminateRequest);
64+
// enable sandbox
65+
$application = $this->sandbox->getLaravelApp();
66+
$this->sandbox->enable();
6167
// check if socket.io connection established
6268
if (! $this->websocketHandler->onOpen($swooleRequest->fd, $illuminateRequest)) {
6369
return;
6470
}
65-
$this->websocket->reset(true)->setSender($swooleRequest->fd);
6671
// trigger 'connect' websocket event
6772
if ($this->websocket->eventExists('connect')) {
68-
$this->callOnConnect($illuminateRequest);
73+
// set sandbox container to websocket pipeline
74+
$this->websocket->setContainer($application);
75+
$this->websocket->call('connect', $illuminateRequest);
6976
}
77+
// disable and recycle sandbox resource
78+
$this->sandbox->disable();
7079
} catch (Exception $e) {
7180
$this->logServerError($e);
7281
}
@@ -93,12 +102,18 @@ public function onMessage(Server $server, Frame $frame)
93102

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

105+
// enable sandbox
106+
$application = $this->sandbox->getLaravelApp();
107+
$this->sandbox->enable();
108+
96109
// dispatch message to registered event callback
97110
if ($this->websocket->eventExists($payload['event'])) {
98111
$this->websocket->call($payload['event'], $payload['data']);
99112
} else {
100113
$this->websocketHandler->onMessage($frame);
101114
}
115+
// disable and recycle sandbox resource
116+
$this->sandbox->disable();
102117
} catch (Exception $e) {
103118
$this->logServerError($e);
104119
}
@@ -279,27 +294,4 @@ protected function normalizePushData(array $data)
279294

280295
return [$opcode, $sender, $fds, $broadcast, $assigned, $event, $message];
281296
}
282-
283-
/**
284-
* Call on connect event callback .
285-
*/
286-
protected function callOnConnect($illuminateRequest)
287-
{
288-
// set currnt request to sandbox
289-
$this->sandbox->setRequest($illuminateRequest);
290-
291-
// get application from sandbox
292-
$application = $this->sandbox->getLaravelApp();
293-
294-
// reset session
295-
if (isset($application['session'])) {
296-
$application['session']->flush();
297-
}
298-
299-
// set sandbox container to websocket pipeline
300-
$this->websocket->setContainer($application);
301-
$this->sandbox->enable();
302-
$this->websocket->call('connect', $illuminateRequest);
303-
$this->sandbox->disable();
304-
}
305297
}

src/Websocket/Websocket.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ public function reset($force = false)
317317

318318
if ($force) {
319319
$this->sender = null;
320+
$this->userId = null;
320321
}
321322

322323
return $this;

tests/Websocket/WebsocketTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ public function testToUser()
193193
$this->assertTrue(in_array(3, $websocket->getTo()));
194194
}
195195

196+
public function testGetUserId()
197+
{
198+
$room = m::mock(RoomContract::class);
199+
$room->shouldReceive('getRooms')
200+
->with($sender = 1)
201+
->once()
202+
->andReturn(['uid_1']);
203+
204+
$websocket = $this->getWebsocket($room)->setSender($sender);
205+
$this->assertEquals($sender, $websocket->getUserId());
206+
}
207+
196208
public function testReset()
197209
{
198210
$websocket = $this->getWebsocket();

0 commit comments

Comments
 (0)