Skip to content

Commit

Permalink
Merge pull request #4 from 1Forge/server-updates
Browse files Browse the repository at this point in the history
Updated Socket Library
  • Loading branch information
bricktownseo authored Sep 11, 2019
2 parents cf1f455 + 30e8aef commit dca3107
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 115 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "oneforge/forexquotes",
"type": "library",
"version": "2.0.10",
"version": "3.0.0",
"description": "Library to fetch and parse realtime Forex quotes and convert currencies",
"keywords": [
"forex",
Expand All @@ -20,7 +20,7 @@
"require": {
"guzzlehttp/guzzle": "6.3.*",
"php": ">=5.6.4",
"wisembly/elephant.io": "^3.3"
"wrench/wrench": "2.0.8"
},
"autoload": {
"psr-0": {
Expand Down
114 changes: 32 additions & 82 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 25 additions & 31 deletions lib/ForexDataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
namespace OneForge\ForexQuotes;

use GuzzleHttp\Client;
use ElephantIO\Client as SocketClient;
use ElephantIO\Engine\SocketIO\Version2X;
use Wrench\Client as WebSocket;

class ForexDataClient
{
Expand All @@ -20,36 +19,23 @@ public function __construct($api_key)
$this->api_key = $api_key;

$this->client = new Client([// Base URI is used with relative requests
'base_uri' => 'http://forex.1forge.com/1.0.3/',
'base_uri' => 'https://api.1forge.com/',
'timeout' => 5.0,
'headers' => ['Content-Type' => 'application/json']]);

$this->last_heartbeat = time();
$this->heartbeat_interval = 15;
}

public function sendHeartbeat()
public function beat()
{
$this->socket_client->emit('heartbeat', ['ok']);
$this->last_heartbeat = time();
$this->socket_client->sendData('beat');
}

public function login()
{
$this->socket_client->emit('login', [$this->api_key]);
}

public function shouldSendHeartbeat()
{
return time() - $this->last_heartbeat > $this->heartbeat_interval;
$this->socket_client->sendData('login|'.$this->api_key);
}

public function handleIncomingMessage($message)
{
if($this->shouldSendHeartbeat())
{
$this->sendHeartbeat();
}
{
switch ($message["event"])
{
case "update":
Expand All @@ -70,6 +56,9 @@ public function handleIncomingMessage($message)
case "post_login_success":
$this->handlePostLoginSuccess();
break;
case "heart":
$this->beat();
break;
case "force_close":
$this->handleMessage("The connection was forced closed by the server");
die();
Expand Down Expand Up @@ -115,26 +104,26 @@ public function subscribeTo($symbols)
{
foreach ((array)$symbols AS $symbol)
{
$this->socket_client->emit('subscribe_to', [$symbol]);
$this->socket_client->sendData("subscribe_to|$symbol");
}
}

public function subscribeToAll()
{
$this->socket_client->emit('subscribe_to_all', []);
$this->socket_client->sendData('subscribe_to_all');
}

public function unsubscribeFrom($symbols)
{
foreach ((array)$symbols AS $symbol)
{
$this->socket_client->emit('unsubscribe_from', [$symbol]);
$this->socket_client->sendData("unsubscribe_from|$symbol");
}
}

public function unsubscribeFromAll()
{
$this->socket_client->emit('unsubscribe_from_all', []);
$this->socket_client->sendData('unsubscribe_from_all');
}

private function handlePostLoginSuccess()
Expand All @@ -153,17 +142,22 @@ public function connect($callback)
{
$this->post_login = $callback;

$this->socket_client = new SocketClient(new Version2X('https://socket.forex.1forge.com:3000'));
$this->socket_client = new WebSocket('wss://api.1forge.com/socket','http://localhost');
$this->socket_client->connect();

$this->socket_client->initialize();
$this->login();

while (true)
{
$message = $this->socket_client->read();
// $this->socket_client->send("Hello WebSocket.org!");

$message = $this->decodeSocketMessage($message);
while(true){
$receive = $this->socket_client->receive();
if(isset($receive) && !empty($receive)){
foreach($receive as $id => $body){
$message = $this->decodeSocketMessage($body);
$this->handleIncomingMessage($message);
}

$this->handleIncomingMessage($message);
}
}
}

Expand Down

0 comments on commit dca3107

Please sign in to comment.