forked from Live-Rates/live-rates.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1,787 changed files
with
137,937 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
|
||
use ElephantIO\Client; | ||
use ElephantIO\Engine\SocketIO\Version2X; | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
|
||
|
||
$client = new Client(new Version2X('https://wss.live-rates.com/')); | ||
|
||
$client->initialize(); | ||
$client->emit('key', ['key' => 'trial']); | ||
|
||
while (true) { | ||
$r = $client->read(); | ||
|
||
$array = json_decode(getBetween($r,"[","]")); | ||
$obj = json_decode($array[1]); | ||
|
||
if (is_null($obj->currency)) { | ||
print_r($array[1]); | ||
print("\n"); | ||
} else { | ||
|
||
echo("Instrument: {$obj->currency} | Ask: {$obj->ask} | Bid: {$obj->bid}"); | ||
print("\n"); | ||
} | ||
} | ||
|
||
$client->close(); | ||
|
||
|
||
|
||
function getBetween($string, $start = "", $end = ""){ | ||
if (strpos($string, $start)) { // required if $start not exist in $string | ||
$startCharCount = strpos($string, $start) + strlen($start)-1; | ||
$firstSubStr = substr($string, $startCharCount, strlen($string)); | ||
$endCharCount = strpos($firstSubStr, $end)+1; | ||
if ($endCharCount == 0) { | ||
$endCharCount = strlen($firstSubStr); | ||
} | ||
return substr($firstSubStr, 0, $endCharCount); | ||
} else { | ||
return ''; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
examples/Websocket-Streaming API/PHP/src/AbstractPayload.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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; | ||
|
||
/** | ||
* Payload for sending data through the websocket | ||
* | ||
* Loosely based on the work of the following : | ||
* - Ludovic Barreca (@ludovicbarreca) | ||
* - Byeoung Wook (@kbu1564) | ||
* | ||
* @link https://tools.ietf.org/html/rfc6455#section-5.2 | ||
* @author Baptiste Clavié <[email protected]> | ||
*/ | ||
abstract class AbstractPayload | ||
{ | ||
const OPCODE_NON_CONTROL_RESERVED_1 = 0x3; | ||
const OPCODE_NON_CONTROL_RESERVED_2 = 0x4; | ||
const OPCODE_NON_CONTROL_RESERVED_3 = 0x5; | ||
const OPCODE_NON_CONTROL_RESERVED_4 = 0x6; | ||
const OPCODE_NON_CONTROL_RESERVED_5 = 0x7; | ||
|
||
const OPCODE_CONTINUE = 0x0; | ||
const OPCODE_TEXT = 0x1; | ||
const OPCODE_BINARY = 0x2; | ||
const OPCODE_CLOSE = 0x8; | ||
const OPCODE_PING = 0x9; | ||
const OPCODE_PONG = 0xA; | ||
|
||
const OPCODE_CONTROL_RESERVED_1 = 0xB; | ||
const OPCODE_CONTROL_RESERVED_2 = 0xC; | ||
const OPCODE_CONTROL_RESERVED_3 = 0xD; | ||
const OPCODE_CONTROL_RESERVED_4 = 0xE; | ||
const OPCODE_CONTROL_RESERVED_5 = 0xF; | ||
|
||
protected $fin = 0b1; // only one frame is necessary | ||
protected $rsv = [0b0, 0b0, 0b0]; // rsv1, rsv2, rsv3 | ||
|
||
protected $mask = false; | ||
protected $maskKey = "\x00\x00\x00\x00"; | ||
|
||
protected $opCode; | ||
|
||
/** | ||
* Mask a data according to the current mask key | ||
* | ||
* @param string $data Data to mask | ||
* @return string Masked data | ||
*/ | ||
protected function maskData($data) | ||
{ | ||
$masked = ''; | ||
$data = \str_split($data); | ||
$key = \str_split($this->maskKey); | ||
|
||
foreach ($data as $i => $letter) { | ||
$masked .= $letter ^ $key[$i % 4]; | ||
} | ||
|
||
return $masked; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?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; | ||
|
||
use Psr\Log\NullLogger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
use ElephantIO\Exception\SocketException; | ||
|
||
/** | ||
* Represents the IO Client which will send and receive the requests to the | ||
* websocket server. It basically suggercoat the Engine used with loggers. | ||
* | ||
* @author Baptiste Clavié <[email protected]> | ||
*/ | ||
class Client | ||
{ | ||
/** @var EngineInterface */ | ||
private $engine; | ||
|
||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
private $isConnected = false; | ||
|
||
public function __construct(EngineInterface $engine, LoggerInterface $logger = null) | ||
{ | ||
$this->engine = $engine; | ||
$this->logger = $logger ?: new NullLogger; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if (!$this->isConnected) { | ||
return; | ||
} | ||
|
||
$this->close(); | ||
} | ||
|
||
/** | ||
* Connects to the websocket | ||
* | ||
* @return $this | ||
*/ | ||
public function initialize() | ||
{ | ||
try { | ||
$this->logger->debug('Connecting to the websocket'); | ||
$this->engine->connect(); | ||
$this->logger->debug('Connected to the server'); | ||
|
||
$this->isConnected = true; | ||
} catch (SocketException $e) { | ||
$this->logger->error('Could not connect to the server', ['exception' => $e]); | ||
|
||
throw $e; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Reads a message from the socket | ||
* | ||
* @return string Message read from the socket | ||
*/ | ||
public function read() | ||
{ | ||
$this->logger->debug('Reading a new message from the socket'); | ||
return $this->engine->read(); | ||
} | ||
|
||
/** | ||
* Emits a message through the engine | ||
* | ||
* @param string $event | ||
* @param array $args | ||
* | ||
* @return $this | ||
*/ | ||
public function emit($event, array $args) | ||
{ | ||
$this->logger->debug('Sending a new message', ['event' => $event, 'args' => $args]); | ||
$this->engine->emit($event, $args); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the namespace for the next messages | ||
* | ||
* @param string namespace the name of the namespace | ||
* @return $this | ||
*/ | ||
public function of($namespace) | ||
{ | ||
$this->logger->debug('Setting the namespace', ['namespace' => $namespace]); | ||
$this->engine->of($namespace); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Closes the connection | ||
* | ||
* @return $this | ||
*/ | ||
public function close() | ||
{ | ||
$this->logger->debug('Closing the connection to the websocket'); | ||
$this->engine->close(); | ||
|
||
$this->isConnected = false; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the engine used, for more advanced functions | ||
* | ||
* @return EngineInterface | ||
*/ | ||
public function getEngine() | ||
{ | ||
return $this->engine; | ||
} | ||
} |
Oops, something went wrong.