|
1 | 1 | <?php |
2 | 2 | namespace Ratchet\Server; |
3 | 3 | use Ratchet\MessageComponentInterface; |
| 4 | +use Ratchet\WebSocket\ReactConnection; |
4 | 5 | use React\EventLoop\LoopInterface; |
5 | 6 | use React\Socket\ServerInterface; |
6 | 7 | use React\EventLoop\Factory as LoopFactory; |
@@ -75,66 +76,71 @@ public function run() { |
75 | 76 | // @codeCoverageIgnoreEnd |
76 | 77 | } |
77 | 78 |
|
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 | + */ |
82 | 84 | 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; |
85 | 88 |
|
86 | | - $uri = $conn->getRemoteAddress(); |
87 | | - $conn->decor->remoteAddress = trim( |
| 89 | + $uri = $reactConn->getRemoteAddress(); |
| 90 | + $reactConn->decor->remoteAddress = trim( |
88 | 91 | parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_HOST), |
89 | 92 | '[]' |
90 | 93 | ); |
91 | 94 |
|
92 | | - $this->app->onOpen($conn->decor); |
| 95 | + $this->app->onOpen($reactConn->decor); |
93 | 96 |
|
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); |
96 | 99 | }); |
97 | | - $conn->on('close', function () use ($conn) { |
98 | | - $this->handleEnd($conn); |
| 100 | + $conn->on('close', function () use ($reactConn) { |
| 101 | + $this->handleEnd($reactConn); |
99 | 102 | }); |
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); |
102 | 105 | }); |
103 | 106 | } |
104 | 107 |
|
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) { |
111 | 115 | try { |
112 | | - $this->app->onMessage($conn->decor, $data); |
| 116 | + $this->app->onMessage($reactConn->decor, $data); |
113 | 117 | } catch (\Exception $e) { |
114 | | - $this->handleError($e, $conn); |
| 118 | + $this->handleError($e, $reactConn); |
115 | 119 | } |
116 | 120 | } |
117 | 121 |
|
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) { |
123 | 128 | try { |
124 | | - $this->app->onClose($conn->decor); |
| 129 | + $this->app->onClose($reactConn->decor); |
125 | 130 | } catch (\Exception $e) { |
126 | | - $this->handleError($e, $conn); |
| 131 | + $this->handleError($e, $reactConn); |
127 | 132 | } |
128 | 133 |
|
129 | | - unset($conn->decor); |
| 134 | + unset($reactConn->decor); |
130 | 135 | } |
131 | 136 |
|
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); |
139 | 145 | } |
140 | 146 | } |
0 commit comments