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

Commit 69282bd

Browse files
committed
improve isWebsocketPushPacket function
1 parent 392052d commit 69282bd

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/Concerns/InteractsWithWebsocket.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ public function onClose($server, $fd, $reactorId)
135135
}
136136
}
137137

138+
/**
139+
* Indicates if a packet is websocket push action.
140+
*
141+
* @param mixed
142+
*/
143+
protected function isWebsocketPushPacket($packet)
144+
{
145+
if ( !is_array($packet)) {
146+
return false;
147+
}
148+
149+
return $this->isWebsocket
150+
&& array_key_exists('action', $packet)
151+
&& $packet['action'] === Websocket::PUSH_ACTION;
152+
}
153+
154+
138155
/**
139156
* Push websocket message to clients.
140157
*

src/Server/Manager.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,8 @@ public function onTask($server, $taskId, $srcWorkerId, $data)
230230

231231
try {
232232
// push websocket message
233-
if (is_array($data)) {
234-
if ($this->isWebsocket
235-
&& array_key_exists('action', $data)
236-
&& $data['action'] === Websocket::PUSH_ACTION) {
237-
$this->pushMessage($server, $data['data'] ?? []);
238-
}
233+
if ($this->isWebsocketPushPacket($data)) {
234+
$this->pushMessage($server, $data['data'] ?? []);
239235
// push async task to queue
240236
} elseif (is_string($data)) {
241237
$decoded = json_decode($data, true);

tests/Server/ManagerTest.php

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

33
namespace SwooleTW\Http\Tests\Server;
44

5+
use Exception;
56
use Mockery as m;
67
use Swoole\Table;
78
use Swoole\Http\Request;
@@ -574,6 +575,9 @@ protected function getContainer($server = null, $config = null)
574575
$container->singleton('swoole.server', function () use ($server) {
575576
return $server;
576577
});
578+
$container->singleton(ExceptionHandler::class, function () {
579+
return new DummyExceptionHandler;
580+
});
577581

578582
return $container;
579583
}
@@ -623,3 +627,27 @@ protected function mockMethod($name, \Closure $function, $namespace = null)
623627
parent::mockMethod($name, $function, 'SwooleTW\Http\Server');
624628
}
625629
}
630+
631+
class DummyExceptionHandler implements ExceptionHandler {
632+
public function report(Exception $e)
633+
{
634+
$this->dump($e);
635+
}
636+
637+
public function render($request, Exception $e)
638+
{
639+
$this->dump($e);
640+
}
641+
642+
public function renderForConsole($output, Exception $e)
643+
{
644+
$this->dump($e);
645+
}
646+
647+
protected function dump($e)
648+
{
649+
echo "\n Server Error: ";
650+
echo $e->getMessage();
651+
die;
652+
}
653+
}

0 commit comments

Comments
 (0)