Skip to content

Commit

Permalink
added webScoket feed
Browse files Browse the repository at this point in the history
Signed-off-by:ghostjat <ghost.jat@gmail.com>
  • Loading branch information
ghostjat committed Jan 7, 2023
1 parent 793f723 commit 872d893
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 25 deletions.
78 changes: 78 additions & 0 deletions examples/wsc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

require 'vendor/autoload.php';

use Core\Shoonya;

$loop = React\EventLoop\Loop::get();
$hs = json_encode(['t' => 'h']);

$api = new Shoonya();
if ($api->login()) {
echo 'Loggedin ....!' . PHP_EOL;
echo 'attemping to connect the webscoket server...' . PHP_EOL;

\Ratchet\Client\connect('wss://api.shoonya.com/NorenWSTP/')->then(function ($conn) use ($loop, $hs, $api) {
$loop->addPeriodicTimer(3000, function () use ($conn, $hs) {
$conn->send($hs);
});

$conn->send($api->connectWS());

$conn->on('message', function ($msg) use ($conn, $api) {
$res = json_decode($msg);
echo "Received: {$res->t}\n";
switch ($res->t) {
case 'ck':
echo 'Conected TO webSocketServer' . PHP_EOL;
$conn->send($api->subscribe('BSE|543257',$api::FeedSnapQuoate));
break;
case 'tk' || 'tf':
echo 'touchline task' . PHP_EOL;
cls();
print_r($res);
break;
case 'dk' || 'df':
file_put_contents('irfc.json', $msg, FILE_APPEND);
system('clear');
print_r($res);
break;
case 'om':
break;
default:
$conn->send($api->unsubscribe('BSE|543257',$api::FeedSnapQuoate));
$conn->close();
break;
}
});
}, function ($e) use ($api) {
echo "Could not connect: {$e->getMessage()}\n";
echo ($api->loadChart()) ? 'logout succesfully' . PHP_EOL : 'failed to logout' . PHP_EOL;
});
#$loop->run();
}

function trigger($data) {
foreach($data as $single) {
$implode[] = implode(', ', $single);
}
echo implode(', ', $implode) .PHP_EOL;
}

function rcvQuotes($data) {

}

function rcvOrders($data) {

}

function open($data) {
global $api;
$instruments = 'NSE|22#BSE|500400';
$api->subscribe($instruments);
}

function cls() {
print("\033[2J\033[;H");
}
62 changes: 37 additions & 25 deletions src/Core/Shoonya.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ class Shoonya {
public const FeedTouchLine = 't', FeedSnapQuoate = 'd';
public const PriceMarket = 'MKT', PriceLimit = 'LMT', PrinceSLLmit = 'SL-LMT', PriceSLM = 'SL-MKT', DS = 'DS', L2 = '2L', L3 = '3L';
public const Buy = 'B', Sell = 'S', AITG = 'LTP_A_O', AITL = 'LTP_B_O';
public const CIQ = 'lib/chartIQ/FCIQ/index.php?', TVC = 'lib/tvcl/?';

public const CIQ = 'lib/chartIQ/FCIQ/index.php?', TVC = 'NorenCharts/?';
public $tmp;
protected $urls = [
'host' => 'https://api.shoonya.com/',
'ws_endpoint' => 'wss://api.shoonya.com/NorenWSTP',
'endpoint' => 'https://api.shoonya.com/NorenWClientTP',
'eodhost' => 'https://api.shoonya.com/chartApi/getdata',
'webchart' => 'https://trade.shoonya.com/',
'localchart' => 'http://127.0.0.1:8080/'
'webchart' =>'https://trade.shoonya.com/',
'localchart' =>'http://127.0.0.1:8080/'
];
protected $routes = [
'login' => '/QuickAuth',
Expand Down Expand Up @@ -67,7 +65,10 @@ class Shoonya {
'cancelalert' => '/CancelAlert',
'modifyalert' => '/ModifyAlert',
'getpendingalert' => '/GetPendingAlert',
'getenabledalert' => '/GetEnabledAlertTypes'
'getenabledalert' => '/GetEnabledAlertTypes',
'spanCalculator' =>'/SpanCalc',
'optionGreek' =>'/GetOptionGreek',
'DPSeries' =>'/EODChartData'
];

public function __construct() {
Expand Down Expand Up @@ -411,11 +412,7 @@ public function getDailyPriceSeries(string $tysm, string $startDate, string $end
'to' => $et
];

$request = $this->guzzle->post($this->urls['eodhost'], [
'header' => ['Content-Type' => 'application/json'],
'body' => json_encode($values)
]);
return $this->decode($request->getBody());
return $this->request('DPSeries', $values);
}

/**
Expand Down Expand Up @@ -799,27 +796,31 @@ public function connectWS() {
'actid' => $this->accountId,
'ordersource' => 'API',
'susertoken' => $this->jKey];
$this->wsC = new \WSSC\WebSocketClient($this->urls['ws_endpoint'], new \WSSC\Components\ClientConfig());
$this->wsC->send(json_encode($value));
print_r($this->wsC->receive());
if ($this->wsC->isConnected()) {
return true;
return json_encode($value);
}
echo 'Failed to connect to WSS' . PHP_EOL;
return false;
}

public function subscribe(array|string $intst, $feedType = self::FeedTouchLine) {
/**
* Subscribe to quotes feeds (websocket)
* @param array|string $intst
* @param type $feedType
*/
public function subscribeFeed(array|string $intst, $feedType = self::FeedTouchLine) {
$values = [];
$values['t'] = $feedType;
if (is_array($intst)) {
$values['k'] = implode('#', $intst);
} else {
$values['k'] = $intst;
}
return json_encode($values);
}

public function unsubscribe(array|string $intst, $feedType = self::FeedTouchLine) {
/**
* Unsubscribe to quotes feed(websocket)
* @param array|string $intst
* @param type $feedType
*/
public function unsubscribeFeed(array|string $intst, $feedType = self::FeedTouchLine) {
$values = [];
if ($feedType == self::FeedTouchLine) {
$values['t'] = 'u';
Expand All @@ -835,12 +836,20 @@ public function unsubscribe(array|string $intst, $feedType = self::FeedTouchLine
}

/**
* @todo
* subscribe to orders feed (websocket)
* @return type
*/
public function subscribeOrders() {
$values = ['t' => 'o', 'actid' => $this->accountId];
public function subscribeOrdersFeed() {
return json_encode(['t' => 'o', 'actid' => $this->accountId]);
}

/**
* unsubscribe orders feed(websocket)
*/
public function unSubscibeOrdersFeed() {
return json_encode(['t'=>'uo','t'=>'uok']);
}

/**
* Set Alert
* @param string $tsym
Expand Down Expand Up @@ -930,7 +939,7 @@ public function loadChart(string $tsym = 'nifty 50', string $exch = 'NSE', strin
($tsym == 'NIFTY50' || $tsym == 'nifty50') ? $tsym = 'NIFTY_50' : $tsym . '-EQ';
$encode = base64_encode('user=' . $this->uid . '&token=' . $this->jKey . "&exch_tsym=NSE:$tsym:$tsym&p=Web");
}
return $this->urls['localchart'] . $chartType . $encode;
return ($chartType == self::TVC) ? $this->urls['webchart'].$chartType.$encode:$this->urls['localchart'].$chartType.$encode;
}

/**
Expand Down Expand Up @@ -1024,4 +1033,7 @@ private function sessionJson() {
return file_put_contents('session.json', json_encode($this->getSessionData(), JSON_PRETTY_PRINT));
}

public function __destruct() {
$this->logout();
}
}

0 comments on commit 872d893

Please sign in to comment.