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

Commit c03c2fd

Browse files
committed
add getUserId and getUser functions to websocket
1 parent 31462ac commit c03c2fd

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/Websocket/Authenticatable.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
namespace SwooleTW\Http\Websocket;
44

55
use InvalidArgumentException;
6+
use Illuminate\Support\Facades\Auth;
67
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
78

89
trait Authenticatable
910
{
11+
protected $user;
12+
protected $userId;
13+
1014
/**
1115
* Login using current user.
1216
*/
@@ -53,6 +57,42 @@ public function toUserId($userIds)
5357
return $this;
5458
}
5559

60+
/**
61+
* Get current auth user by sender's fd.
62+
*/
63+
public function getUser()
64+
{
65+
if ($this->user instanceof AuthenticatableContract) {
66+
return $this->user;
67+
}
68+
69+
if (! is_null($uid = $this->getUserId()) && $user = Auth::loginUsingId($uid)) {
70+
$this->user = $user;
71+
}
72+
73+
return $this->user;
74+
}
75+
76+
/**
77+
* Get current auth user id by sender's fd.
78+
*/
79+
public function getUserId()
80+
{
81+
if (! is_null($this->userId)) {
82+
return $this->userId;
83+
}
84+
85+
$rooms = $this->room->getRooms($this->getSender());
86+
87+
foreach ($rooms as $room) {
88+
if (count($explode = explode(static::USER_PREFIX, $room)) === 2) {
89+
$this->userId = $explode[1];
90+
}
91+
}
92+
93+
return $this->userId;
94+
}
95+
5696
/**
5797
* Check if user object implements AuthenticatableContract.
5898
*/

src/Websocket/Websocket.php

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

318318
if ($force) {
319319
$this->sender = null;
320+
$this->user = null;
321+
$this->userId = null;
320322
}
321323

322324
return $this;

tests/Websocket/WebsocketTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use Illuminate\Pipeline\Pipeline;
99
use SwooleTW\Http\Tests\TestCase;
10+
use Illuminate\Support\Facades\Auth;
1011
use SwooleTW\Http\Websocket\Websocket;
1112
use SwooleTW\Http\Websocket\Rooms\RoomContract;
1213
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
@@ -193,6 +194,36 @@ public function testToUser()
193194
$this->assertTrue(in_array(3, $websocket->getTo()));
194195
}
195196

197+
public function testGetUserId()
198+
{
199+
$room = m::mock(RoomContract::class);
200+
$room->shouldReceive('getRooms')
201+
->with($sender = 1)
202+
->once()
203+
->andReturn(['uid_1']);
204+
205+
$websocket = $this->getWebsocket($room)->setSender($sender);
206+
$this->assertEquals($sender, $websocket->getUserId());
207+
}
208+
209+
public function testGetUser()
210+
{
211+
$room = m::mock(RoomContract::class);
212+
$room->shouldReceive('getRooms')
213+
->with($sender = 1)
214+
->once()
215+
->andReturn(['uid_1']);
216+
217+
$user = m::mock(AuthenticatableContract::class);
218+
Auth::shouldReceive('loginUsingId')
219+
->with($sender)
220+
->once()
221+
->andReturn($user);
222+
223+
$websocket = $this->getWebsocket($room)->setSender($sender);
224+
$this->assertEquals($user, $websocket->getUser());
225+
}
226+
196227
public function testReset()
197228
{
198229
$websocket = $this->getWebsocket();

0 commit comments

Comments
 (0)