Skip to content

Commit 23cc250

Browse files
xuanyanwowhzh
andauthored
Added debug log. Return result about websocket senderpushdisconnect. (#6648)
* Added debug log. Added return value about ws sender. * Update CHANGELOG-3.1.md --------- Co-authored-by: hzh <hzh@addcn.com>
1 parent 87043e5 commit 23cc250

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/Sender.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
use function Hyperf\Engine\swoole_get_flags_from_frame;
2828

2929
/**
30-
* @method void push(int $fd, $data, int $opcode = null, $finish = null)
31-
* @method void disconnect(int $fd, int $code = null, string $reason = null)
30+
* @method bool push(int $fd, $data, int $opcode = null, $finish = null)
31+
* @method bool disconnect(int $fd, int $code = null, string $reason = null)
3232
*/
3333
class Sender
3434
{
@@ -62,15 +62,22 @@ public function __call($name, $arguments)
6262
$method = 'close';
6363
}
6464

65-
$this->responses[$fd]->{$method}(...$arguments);
66-
$this->logger->debug("[WebSocket] Worker send to #{$fd}");
65+
$result = $this->responses[$fd]->{$method}(...$arguments);
66+
$this->logger->debug(
67+
sprintf(
68+
"[WebSocket] Worker send to #{$fd}.Send %s",
69+
$result ? 'success' : 'failed'
70+
)
71+
);
72+
return $result;
6773
}
68-
return;
74+
return false;
6975
}
7076

7177
if (! $this->proxy($fd, $method, $arguments)) {
7278
$this->sendPipeMessage($name, $arguments);
7379
}
80+
return true;
7481
}
7582

7683
public function pushFrame(int $fd, FrameInterface $frame): bool
@@ -97,8 +104,13 @@ public function proxy(int $fd, string $method, array $arguments): bool
97104
if ($result) {
98105
/** @var \Swoole\WebSocket\Server $server */
99106
$server = $this->getServer();
100-
$server->{$method}(...$arguments);
101-
$this->logger->debug("[WebSocket] Worker.{$this->workerId} send to #{$fd}");
107+
$result = $server->{$method}(...$arguments);
108+
$this->logger->debug(
109+
sprintf(
110+
"[WebSocket] Worker.{$this->workerId} send to #{$fd}.Send %s",
111+
$result ? 'success' : 'failed'
112+
)
113+
);
102114
}
103115

104116
return $result;

tests/SenderTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\Context\ApplicationContext;
1717
use Hyperf\Contract\ConfigInterface;
1818
use Hyperf\Contract\StdoutLoggerInterface;
19+
use Hyperf\Server\CoroutineServer;
1920
use Hyperf\WebSocketServer\Sender;
2021
use HyperfTest\ModelCache\Stub\StdoutLogger;
2122
use Mockery;
@@ -53,13 +54,44 @@ public function testSenderCheck()
5354
$this->assertTrue($sender->check(1));
5455
}
5556

56-
protected function getContainer()
57+
public function testSenderResult()
58+
{
59+
$container = $this->getContainer([
60+
'server' => [
61+
'type' => CoroutineServer::class,
62+
],
63+
]);
64+
$sender = new Sender($container);
65+
$sender->setResponse(1, new class (){
66+
public function push($data)
67+
{
68+
return true;
69+
}
70+
71+
public function close()
72+
{
73+
return true;
74+
}
75+
});
76+
77+
$res = $sender->push(1, 'data');
78+
$this->assertTrue($res);
79+
$res = $sender->disconnect(1);
80+
$this->assertTrue($res);
81+
82+
$res = $sender->push(2, 'data');
83+
$this->assertFalse($res);
84+
$res = $sender->disconnect(2);
85+
$this->assertFalse($res);
86+
}
87+
88+
protected function getContainer(array $config = [])
5789
{
5890
$container = Mockery::mock(ContainerInterface::class);
5991
ApplicationContext::setContainer($container);
6092

6193
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
62-
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([]));
94+
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config($config));
6395

6496
return $container;
6597
}

0 commit comments

Comments
 (0)