-
Notifications
You must be signed in to change notification settings - Fork 366
Add Socket.IO v2 support #152
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,9 @@ protected function handshake() | |
throw new ServerConnectionFailureException; | ||
} | ||
|
||
$decoded = json_decode(substr($result, strpos($result, '{')), true); | ||
$open_curly_at = strpos($result, '{'); | ||
$todecode = substr($result, $open_curly_at, strrpos($result, '}')-$open_curly_at+1); | ||
$decoded = json_decode($todecode, true); | ||
|
||
if (!in_array('websocket', $decoded['upgrades'])) { | ||
throw new UnsupportedTransportException('websocket'); | ||
|
@@ -185,15 +187,17 @@ protected function handshake() | |
} | ||
|
||
/** Upgrades the transport to WebSocket */ | ||
private function upgradeTransport() | ||
protected function upgradeTransport() | ||
{ | ||
$query = ['sid' => $this->session->id, | ||
'EIO' => $this->options['version'], | ||
'use_b64' => $this->options['use_b64'], | ||
'transport' => static::TRANSPORT_WEBSOCKET]; | ||
|
||
if ($this->options['version'] === 2) | ||
$query['use_b64'] = $this->options['use_b64']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing braces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (PSR and all that) |
||
|
||
$url = sprintf('/%s/?%s', trim($this->url['path'], '/'), http_build_query($query)); | ||
$key = base64_encode(sha1(uniqid(mt_rand(), true), true)); | ||
$key = base64_encode(openssl_random_pseudo_bytes(16)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not good either, as it requires openssl. If you absolutely need 20 bytes encoded, you can pass the second argument of sha1 to |
||
|
||
$origin = '*'; | ||
$headers = isset($this->context['headers']) ? (array) $this->context['headers'] : [] ; | ||
|
@@ -208,7 +212,7 @@ private function upgradeTransport() | |
} | ||
|
||
$request = "GET {$url} HTTP/1.1\r\n" | ||
. "Host: {$this->url['host']}\r\n" | ||
. "Host: {$this->url['host']}:{$this->url['port']}\r\n" | ||
. "Upgrade: WebSocket\r\n" | ||
. "Connection: Upgrade\r\n" | ||
. "Sec-WebSocket-Key: {$key}\r\n" | ||
|
@@ -234,7 +238,8 @@ private function upgradeTransport() | |
$this->write(EngineInterface::UPGRADE); | ||
|
||
//remove message '40' from buffer, emmiting by socket.io after receiving EngineInterface::UPGRADE | ||
$this->read(); | ||
if ($this->options['version'] === 2) | ||
$this->read(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* This file is part of the Elephant.io package | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
* | ||
* @copyright Wisembly | ||
* @license http://www.opensource.org/licenses/MIT-License MIT License | ||
*/ | ||
|
||
namespace ElephantIO\Engine\SocketIO; | ||
|
||
use DomainException; | ||
use InvalidArgumentException; | ||
use UnexpectedValueException; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
use ElephantIO\EngineInterface; | ||
use ElephantIO\Payload\Encoder; | ||
use ElephantIO\Engine\AbstractSocketIO; | ||
|
||
use ElephantIO\Exception\SocketException; | ||
use ElephantIO\Exception\UnsupportedTransportException; | ||
use ElephantIO\Exception\ServerConnectionFailureException; | ||
|
||
/** | ||
* Implements the dialog with Socket.IO version 2.x | ||
* | ||
* Based on the work of Mathieu Lallemand (@lalmat) | ||
* | ||
* @author Baptiste Clavié <baptiste@wisembly.com> | ||
* @link https://tools.ietf.org/html/rfc6455#section-5.2 Websocket's RFC | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comment is not right |
||
class Version2X extends Version1X | ||
{ | ||
|
||
/** {@inheritDoc} */ | ||
public function getName() | ||
{ | ||
return 'SocketIO Version 2.X'; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
protected function getDefaultOptions() | ||
{ | ||
$defaults = parent::getDefaultOptions(); | ||
|
||
$defaults['version'] = 3; | ||
|
||
return $defaults; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tab to space please (code style)