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

Commit 6f43269

Browse files
authored
Merge pull request #338 from swooletw/master
merge master back to develop
2 parents 9caffc7 + 8b6ffc0 commit 6f43269

File tree

9 files changed

+121
-54
lines changed

9 files changed

+121
-54
lines changed

src/Server/Manager.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,25 @@ protected function initialize()
114114
{
115115
$this->createTables();
116116
$this->prepareWebsocket();
117-
$this->setSwooleServerListeners();
117+
118+
if (! $this->container->make(Server::class)->taskworker) {
119+
$this->setSwooleServerListeners();
120+
}
118121
}
119122

120123
/**
121124
* Set swoole server listeners.
122125
*/
123126
protected function setSwooleServerListeners()
124127
{
128+
$server = $this->container->make(Server::class);
125129
foreach ($this->events as $event) {
126130
$listener = Str::camel("on_$event");
127131
$callback = method_exists($this, $listener) ? [$this, $listener] : function () use ($event) {
128132
$this->container->make('events')->dispatch("swoole.$event", func_get_args());
129133
};
130134

131-
$this->container->make(Server::class)->on($event, $callback);
135+
$server->on($event, $callback);
132136
}
133137
}
134138

@@ -170,13 +174,7 @@ public function onWorkerStart($server)
170174

171175
$this->container->make('events')->dispatch('swoole.workerStart', func_get_args());
172176

173-
// don't init laravel app in task workers
174-
if ($server->taskworker) {
175-
$this->setProcessName('task process');
176-
177-
return;
178-
}
179-
$this->setProcessName('worker process');
177+
$this->setProcessName($server->taskworker ? 'task process' : 'worker process');
180178

181179
// clear events instance in case of repeated listeners in worker process
182180
Facade::clearResolvedInstance('events');

src/Websocket/Facades/Room.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @method static this prepare()
9-
* @method static this add($fd, $rooms)
10-
* @method static this delete($fd, $rooms)
8+
* @method static $this prepare()
9+
* @method static $this add($fd, $rooms)
10+
* @method static $this delete($fd, $rooms)
1111
* @method static array getClients($room)
1212
* @method static array getRooms($fd)
1313
*
@@ -24,4 +24,4 @@ protected static function getFacadeAccessor()
2424
{
2525
return 'swoole.room';
2626
}
27-
}
27+
}

src/Websocket/Facades/Websocket.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @method static this broadcast()
9-
* @method static this to($values)
10-
* @method static this join($rooms)
11-
* @method static this leave($rooms)
8+
* @method static $this broadcast()
9+
* @method static $this to($values)
10+
* @method static $this join($rooms)
11+
* @method static $this leave($rooms)
1212
* @method static boolean emit($event, $data)
13-
* @method static this in($room)
14-
* @method static this on($event, $callback)
13+
* @method static $this in($room)
14+
* @method static $this on($event, $callback)
1515
* @method static boolean eventExists($event)
1616
* @method static mixed call($event, $data)
1717
* @method static boolean close($fd)
18-
* @method static this setSender($fd)
18+
* @method static $this setSender($fd)
1919
* @method static int getSender()
2020
* @method static boolean getIsBroadcast()
2121
* @method static array getTo()
22-
* @method static this reset()
23-
* @method static this middleware($middleware)
24-
* @method static this setContainer($container)
25-
* @method static this setPipeline($pipeline)
22+
* @method static $this reset()
23+
* @method static $this middleware($middleware)
24+
* @method static $this setContainer($container)
25+
* @method static $this setPipeline($pipeline)
2626
* @method static \Illuminate\Contracts\Pipeline\Pipeline getPipeline()
2727
* @method static mixed loginUsing($user)
28-
* @method static this loginUsingId($userId)
29-
* @method static this logout()
30-
* @method static this toUser($users)
31-
* @method static this toUserId($userIds)
28+
* @method static $this loginUsingId($userId)
29+
* @method static $this logout()
30+
* @method static $this toUser($users)
31+
* @method static $this toUserId($userIds)
3232
* @method static string getUserId()
3333
* @method static boolean isUserIdOnline($userId)
3434
*
@@ -45,4 +45,4 @@ protected static function getFacadeAccessor()
4545
{
4646
return 'swoole.websocket';
4747
}
48-
}
48+
}

src/Websocket/Middleware/Authenticate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct(Auth $auth)
4242
public function handle($request, Closure $next)
4343
{
4444
try {
45+
$this->auth->setRequest($request);
4546
if ($user = $this->auth->authenticate()) {
4647
$request->setUserResolver(function () use ($user) {
4748
return $user;

src/Websocket/Rooms/RedisRoom.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ public function getValue(string $key, string $table)
219219
{
220220
$this->checkTable($table);
221221

222-
return $this->redis->smembers($this->getKey($key, $table));
222+
$result = $this->redis->smembers($this->getKey($key, $table));
223+
224+
// Try to fix occasional non-array returned result
225+
return is_array($result) ? $result : [];
223226
}
224227

225228
/**

src/Websocket/Websocket.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Config;
99
use InvalidArgumentException;
1010
use SwooleTW\Http\Server\Facades\Server;
11+
use SwooleTW\Http\Server\Manager;
1112
use SwooleTW\Http\Websocket\Rooms\RoomContract;
1213

1314
/**
@@ -172,17 +173,25 @@ public function emit(string $event, $data): bool
172173
return false;
173174
}
174175

175-
$result = App::make(Server::class)->task([
176-
'action' => static::PUSH_ACTION,
177-
'data' => [
178-
'sender' => $this->sender,
179-
'fds' => $fds,
180-
'broadcast' => $this->isBroadcast,
181-
'assigned' => $assigned,
182-
'event' => $event,
183-
'message' => $data,
184-
],
185-
]);
176+
$payload = [
177+
'sender' => $this->sender,
178+
'fds' => $fds,
179+
'broadcast' => $this->isBroadcast,
180+
'assigned' => $assigned,
181+
'event' => $event,
182+
'message' => $data,
183+
];
184+
185+
$result = true;
186+
$server = App::make(Server::class);
187+
if ($server->taskworker) {
188+
App::make(Manager::class)->pushMessage($server, $payload);
189+
} else {
190+
$result = $server->task([
191+
'action' => static::PUSH_ACTION,
192+
'data' => $payload
193+
]);
194+
}
186195

187196
$this->reset();
188197

tests/Server/ManagerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public function testOnTaskWorkerStart()
178178
return $this->getEvent('swoole.workerStart');
179179
});
180180

181-
$manager = $this->getManager($container);
181+
$path = __DIR__ . '/../fixtures';
182+
$manager = $this->getManager($container, $framework = 'laravel', $path);
182183

183184
$this->assertNull($manager->onWorkerStart($server));
184185
}

tests/Websocket/Middleware/AuthenticateTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ class AuthenticateTest extends TestCase
1212
{
1313
public function testAuthenticate()
1414
{
15+
$request = m::mock(Request::class);
16+
$request->shouldReceive('setUserResolver')
17+
->once();
18+
1519
$auth = m::mock(Auth::class);
1620
$auth->shouldReceive('authenticate')
1721
->once()
1822
->andReturn('user');
19-
20-
$request = m::mock(Request::class);
21-
$request->shouldReceive('setUserResolver')
22-
->once();
23+
$auth->shouldReceive('setRequest')
24+
->with($request)
25+
->once();
2326

2427
$middleware = new Authenticate($auth);
2528
$middleware->handle($request, function ($next) {

tests/Websocket/WebsocketTest.php

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace SwooleTW\Http\Tests\Websocket;
44

5-
use Illuminate\Container\Container;
6-
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
5+
use Mockery as m;
76
use Illuminate\Http\Request;
7+
use InvalidArgumentException;
88
use Illuminate\Pipeline\Pipeline;
9+
use SwooleTW\Http\Server\Manager;
10+
use SwooleTW\Http\Tests\TestCase;
11+
use Illuminate\Container\Container;
912
use Illuminate\Support\Facades\App;
1013
use Illuminate\Support\Facades\Config;
11-
use InvalidArgumentException;
12-
use Mockery as m;
14+
use SwooleTW\Http\Websocket\Websocket;
1315
use SwooleTW\Http\Server\Facades\Server;
14-
use SwooleTW\Http\Tests\TestCase;
1516
use SwooleTW\Http\Websocket\Rooms\RoomContract;
16-
use SwooleTW\Http\Websocket\Websocket;
17+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
1718

1819
class WebsocketTest extends TestCase
1920
{
@@ -326,12 +327,15 @@ public function testEmit()
326327
->times(3)
327328
->andReturn([3, 4, 5]);
328329

330+
$server = m::mock('server');
331+
$server->taskworker = false;
332+
329333
App::shouldReceive('make')
330334
->with(Server::class)
331335
->once()
332-
->andReturnSelf();
336+
->andReturn($server);
333337

334-
App::shouldReceive('task')
338+
$server->shouldReceive('task')
335339
->with([
336340
'action' => 'push',
337341
'data' => [
@@ -355,6 +359,54 @@ public function testEmit()
355359
$this->assertFalse($websocket->getIsBroadcast());
356360
}
357361

362+
public function testEmitInTaskWorker()
363+
{
364+
$sender = 1;
365+
$to = [1, 2, 'a', 'b', 'c'];
366+
$broadcast = true;
367+
$room = m::mock(RoomContract::class);
368+
$room->shouldReceive('getClients')
369+
->with(m::type('string'))
370+
->times(3)
371+
->andReturn([3, 4, 5]);
372+
373+
$payload = [
374+
'sender' => $sender,
375+
'fds' => [1, 2, 3, 4, 5],
376+
'broadcast' => $broadcast,
377+
'assigned' => true,
378+
'event' => $event = 'event',
379+
'message' => $data = 'data',
380+
];
381+
382+
$server = m::mock('server');
383+
$server->taskworker = true;
384+
385+
$manager = m::mock(Manager::class);
386+
$manager->shouldReceive('pushMessage')
387+
->with($server, $payload)
388+
->once();
389+
390+
App::shouldReceive('make')
391+
->with(Server::class)
392+
->once()
393+
->andReturn($server);
394+
395+
App::shouldReceive('make')
396+
->with(Manager::class)
397+
->once()
398+
->andReturn($manager);
399+
400+
$websocket = $this->getWebsocket($room);
401+
$websocket->setSender($sender)
402+
->to($to)
403+
->broadcast()
404+
->emit($event, $data);
405+
406+
$this->assertSame([], $websocket->getTo());
407+
$this->assertFalse($websocket->getIsBroadcast());
408+
}
409+
358410
public function testClose()
359411
{
360412
$fd = 1;

0 commit comments

Comments
 (0)