Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRow committed Jul 4, 2020
1 parent 18a9918 commit bd9a599
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
47 changes: 30 additions & 17 deletions src/NanoWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NanoWS
private $hostname;
private $port;
private $url;
private $options;
private $protocol;
private $id = 0;

Expand All @@ -22,20 +23,45 @@ class NanoWS
// ## Initialization
// #

public function __construct(string $hostname = 'localhost', int $port = 7078, string $url = null)
public function __construct(string $hostname = 'localhost', int $port = 7078, string $url = null, array $options = null)
{
// Hostname
if (strpos($hostname, 'ws://') === 0) {
$hostname = substr($hostname, 5);
}
if (strpos($hostname, 'wss://') === 0) {
$hostname = substr($hostname, 6);
}

// Url
if (!empty($url)) {
if (strpos($url, '/') === 0) {
$url = substr($url, 1);
}
}

$this->options = [];

// Timeout
if (isset($options['timeout'])) {
$this->options['timeout'] = (float) $options['timeout'];
}

// Fragment size
if (isset($options['fragment_size'])) {
$this->options['fragment_size'] = (int) $options['fragment_size'];
}

// Context
if (isset($options['context']) && is_array($options['context'])) {
$this->options['context'] = stream_context_create($options['context']);
}

// Headers
if (isset($options['headers']) && is_array($options['headers'])) {
$this->options['headers'] = $options['headers'];
}

$this->hostname = $hostname;
$this->port = $port;
$this->url = $url;
Expand Down Expand Up @@ -63,23 +89,10 @@ public function setProtocol(string $protocol)
// ## Open connection
// #

public function open(int $timeout = 5, int $fragment_size = 4096, array $context = null, array $headers = null)
{
$options = [
'timeout' => $timeout,
'fragment_size' => $fragment_size
];

if ($context != null) {
$options['context'] = stream_context_create($context);
}

if ($headers != null) {
$options['headers'] = $headers;
}

public function open()
{
try {
$this->websocket = new \WebSocket\Client("{$this->protocol}://{$this->hostname}:{$this->port}/{$this->url}", $options);
$this->websocket = new \WebSocket\Client("{$this->protocol}://{$this->hostname}:{$this->port}/{$this->url}", $this->options);
return true;
} catch (\WebSocket\ConnectionException $e) {
$this->websocket = null;
Expand Down
2 changes: 2 additions & 0 deletions test/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

$nanorpc->setAPI(2);

$account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse';

$t0 = microtime(true);

$nanorpc->AccountWeight(['account' => $account]);
Expand Down
2 changes: 1 addition & 1 deletion test/NanoRPCExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

$account = 'nano_3dyo9e7wkf8kuykghbjdt78njux3yudhdrhtwaymc8fsmxhxpt1h48zffbse';

$nanorpcext->account_balance(['account' => $account]);
$nanorpcext->account_weight(['account' => $account]);

var_dump($nanorpcext);

0 comments on commit bd9a599

Please sign in to comment.