@@ -167,7 +167,9 @@ protected function handshake()
167167 throw new ServerConnectionFailureException ;
168168 }
169169
170- $ decoded = json_decode (substr ($ result , strpos ($ result , '{ ' )), true );
170+ $ open_curly_at = strpos ($ result , '{ ' );
171+ $ todecode = substr ($ result , $ open_curly_at , strrpos ($ result , '} ' )-$ open_curly_at +1 );
172+ $ decoded = json_decode ($ todecode , true );
171173
172174 if (!in_array ('websocket ' , $ decoded ['upgrades ' ])) {
173175 throw new UnsupportedTransportException ('websocket ' );
@@ -184,16 +186,27 @@ protected function handshake()
184186 $ this ->session = new Session ($ decoded ['sid ' ], $ decoded ['pingInterval ' ], $ decoded ['pingTimeout ' ], $ decoded ['upgrades ' ]);
185187 }
186188
187- /** Upgrades the transport to WebSocket */
188- private function upgradeTransport ()
189+ /**
190+ * Upgrades the transport to WebSocket
191+ *
192+ * FYI:
193+ * Version "2" is used for the EIO param by socket.io v1
194+ * Version "3" is used by socket.io v2
195+ */
196+ protected function upgradeTransport ()
189197 {
190198 $ query = ['sid ' => $ this ->session ->id ,
191199 'EIO ' => $ this ->options ['version ' ],
192- 'use_b64 ' => $ this ->options ['use_b64 ' ],
193200 'transport ' => static ::TRANSPORT_WEBSOCKET ];
194201
202+ if ($ this ->options ['version ' ] === 2 )
203+ $ query ['use_b64 ' ] = $ this ->options ['use_b64 ' ];
204+
195205 $ url = sprintf ('/%s/?%s ' , trim ($ this ->url ['path ' ], '/ ' ), http_build_query ($ query ));
196- $ key = base64_encode (sha1 (uniqid (mt_rand (), true ), true ));
206+ $ hash = sha1 (uniqid (mt_rand (), true ), true );
207+ if ($ this ->options ['version ' ] !== 2 )
208+ $ hash = substr ($ hash , 0 , 16 );
209+ $ key = base64_encode ($ hash );
197210
198211 $ origin = '* ' ;
199212 $ headers = isset ($ this ->context ['headers ' ]) ? (array ) $ this ->context ['headers ' ] : [] ;
@@ -208,7 +221,7 @@ private function upgradeTransport()
208221 }
209222
210223 $ request = "GET {$ url } HTTP/1.1 \r\n"
211- . "Host: {$ this ->url ['host ' ]}\r\n"
224+ . "Host: {$ this ->url ['host ' ]}: { $ this -> url [ ' port ' ]} \r\n"
212225 . "Upgrade: WebSocket \r\n"
213226 . "Connection: Upgrade \r\n"
214227 . "Sec-WebSocket-Key: {$ key }\r\n"
@@ -234,7 +247,8 @@ private function upgradeTransport()
234247 $ this ->write (EngineInterface::UPGRADE );
235248
236249 //remove message '40' from buffer, emmiting by socket.io after receiving EngineInterface::UPGRADE
237- $ this ->read ();
250+ if ($ this ->options ['version ' ] === 2 )
251+ $ this ->read ();
238252 }
239253}
240254
0 commit comments