Skip to content

Commit 7a6f62c

Browse files
committed
fix #76
1 parent a3f3c46 commit 7a6f62c

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/concerns/InteractsWithWebsocket.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,23 @@ public function onMessage(TcpConnection $connection, Frame $frame)
8787

8888
public function onClose(TcpConnection $connection)
8989
{
90-
$this->runInSandbox(function (App $app, Websocket $websocket) use ($connection) {
91-
$handler = $app->make(HandlerInterface::class);
92-
try {
93-
$handler->onClose();
94-
} catch (Throwable $e) {
95-
$this->logServerError($e);
96-
}
90+
$this->runInSandbox(function (App $app) use ($connection) {
91+
if ($app->exists(Websocket::class)) {
92+
$websocket = $app->make(Websocket::class);
93+
$handler = $app->make(HandlerInterface::class);
94+
try {
95+
$handler->onClose();
96+
} catch (Throwable $e) {
97+
$this->logServerError($e);
98+
}
9799

98-
// leave all rooms
99-
$websocket->leave();
100+
// leave all rooms
101+
$websocket->leave();
100102

101-
unset($this->messageSender[$connection->id]);
103+
unset($this->messageSender[$connection->id]);
102104

103-
$websocket->setConnected(false);
105+
$websocket->setConnected(false);
106+
}
104107
}, $connection);
105108
}
106109

tests/feature/WebsocketTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use GuzzleHttp\Client;
34
use React\EventLoop\Loop;
45
use Symfony\Component\Process\Process;
56
use function Ratchet\Client\connect;
@@ -28,6 +29,24 @@
2829
$process->stop();
2930
});
3031

32+
beforeEach(function () {
33+
$this->httpClient = new Client([
34+
'base_uri' => 'http://127.0.0.1:8080',
35+
'cookies' => true,
36+
'http_errors' => false,
37+
'timeout' => 1,
38+
]);
39+
});
40+
41+
it('http', function () {
42+
$response = $this->httpClient->get('/');
43+
44+
expect($response->getStatusCode())
45+
->toBe(200)
46+
->and($response->getBody()->getContents())
47+
->toBe('hello world');
48+
});
49+
3150
it('websocket', function () {
3251
$connected = 0;
3352
$messages = [];

0 commit comments

Comments
 (0)