Skip to content

Commit 062d386

Browse files
committed
copied from ratchetphp#1065
1 parent 28b9caa commit 062d386

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

src/Ratchet/Server/IoConnection.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
<?php
22
namespace Ratchet\Server;
33
use Ratchet\ConnectionInterface;
4-
use React\Socket\ConnectionInterface as ReactConn;
4+
use Ratchet\WebSocket\ReactConnection;
55

66
/**
77
* {@inheritdoc}
88
*/
99
class IoConnection implements ConnectionInterface {
1010
/**
11-
* @var \React\Socket\ConnectionInterface
11+
* @var ReactConnection
1212
*/
1313
protected $conn;
14-
14+
/**
15+
* Explicitly define properties to prevent dynamic property deprecation notices
16+
*/
17+
public $resourceId;
18+
public $remoteAddress;
19+
public $httpHeadersReceived;
20+
public $httpBuffer;
21+
public $httpRequest;
22+
public $WebSocket;
1523

1624
/**
17-
* @param \React\Socket\ConnectionInterface $conn
25+
* @param ReactConnection $conn
1826
*/
19-
public function __construct(ReactConn $conn) {
27+
public function __construct(ReactConnection $conn) {
2028
$this->conn = $conn;
2129
}
2230

src/Ratchet/Server/IoServer.php

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Ratchet\Server;
33
use Ratchet\MessageComponentInterface;
4+
use Ratchet\WebSocket\ReactConnection;
45
use React\EventLoop\LoopInterface;
56
use React\Socket\ServerInterface;
67
use React\EventLoop\Factory as LoopFactory;
@@ -75,66 +76,71 @@ public function run() {
7576
// @codeCoverageIgnoreEnd
7677
}
7778

78-
/**
79-
* Triggered when a new connection is received from React
80-
* @param \React\Socket\ConnectionInterface $conn
81-
*/
79+
/**
80+
* Triggered when a new connection is received from React
81+
* @param \React\Socket\ConnectionInterface $conn
82+
* @throws \Exception
83+
*/
8284
public function handleConnect($conn) {
83-
$conn->decor = new IoConnection($conn);
84-
$conn->decor->resourceId = (int)$conn->stream;
85+
$reactConn = new ReactConnection($conn->stream, $this->loop);
86+
$reactConn->decor = new IoConnection($reactConn);
87+
$reactConn->decor->resourceId = (int)$reactConn->stream;
8588

86-
$uri = $conn->getRemoteAddress();
87-
$conn->decor->remoteAddress = trim(
89+
$uri = $reactConn->getRemoteAddress();
90+
$reactConn->decor->remoteAddress = trim(
8891
parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST),
8992
'[]'
9093
);
9194

92-
$this->app->onOpen($conn->decor);
95+
$this->app->onOpen($reactConn->decor);
9396

94-
$conn->on('data', function ($data) use ($conn) {
95-
$this->handleData($data, $conn);
97+
$conn->on('data', function ($data) use ($reactConn) {
98+
$this->handleData($data, $reactConn);
9699
});
97-
$conn->on('close', function () use ($conn) {
98-
$this->handleEnd($conn);
100+
$conn->on('close', function () use ($reactConn) {
101+
$this->handleEnd($reactConn);
99102
});
100-
$conn->on('error', function (\Exception $e) use ($conn) {
101-
$this->handleError($e, $conn);
103+
$conn->on('error', function (\Exception $e) use ($reactConn) {
104+
$this->handleError($e, $reactConn);
102105
});
103106
}
104107

105-
/**
106-
* Data has been received from React
107-
* @param string $data
108-
* @param \React\Socket\ConnectionInterface $conn
109-
*/
110-
public function handleData($data, $conn) {
108+
/**
109+
* Data has been received from React
110+
* @param string $data
111+
* @param ReactConnection $reactConn
112+
* @throws \Exception
113+
*/
114+
public function handleData($data, $reactConn) {
111115
try {
112-
$this->app->onMessage($conn->decor, $data);
116+
$this->app->onMessage($reactConn->decor, $data);
113117
} catch (\Exception $e) {
114-
$this->handleError($e, $conn);
118+
$this->handleError($e, $reactConn);
115119
}
116120
}
117121

118-
/**
119-
* A connection has been closed by React
120-
* @param \React\Socket\ConnectionInterface $conn
121-
*/
122-
public function handleEnd($conn) {
122+
/**
123+
* A connection has been closed by React
124+
* @param $reactConn
125+
* @throws \Exception
126+
*/
127+
public function handleEnd($reactConn) {
123128
try {
124-
$this->app->onClose($conn->decor);
129+
$this->app->onClose($reactConn->decor);
125130
} catch (\Exception $e) {
126-
$this->handleError($e, $conn);
131+
$this->handleError($e, $reactConn);
127132
}
128133

129-
unset($conn->decor);
134+
unset($reactConn->decor);
130135
}
131136

132-
/**
133-
* An error has occurred, let the listening application know
134-
* @param \Exception $e
135-
* @param \React\Socket\ConnectionInterface $conn
136-
*/
137-
public function handleError(\Exception $e, $conn) {
138-
$this->app->onError($conn->decor, $e);
137+
/**
138+
* An error has occurred, let the listening application know
139+
* @param \Exception $e
140+
* @param $reactConn
141+
* @throws \Exception
142+
*/
143+
public function handleError(\Exception $e, $reactConn) {
144+
$this->app->onError($reactConn->decor, $e);
139145
}
140146
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ratchet\WebSocket;
4+
use React\Socket\Connection;
5+
use React\EventLoop\LoopInterface;
6+
7+
class ReactConnection extends Connection {
8+
public $decor;
9+
public $stream;
10+
11+
public function __construct($stream, $loop) {
12+
parent::__construct($stream, $loop);
13+
}
14+
}

0 commit comments

Comments
 (0)