From 872d893d069a9911dfc9103a1751d950e398b1fe Mon Sep 17 00:00:00 2001 From: ghostjat Date: Sat, 7 Jan 2023 21:29:18 +0530 Subject: [PATCH] added webScoket feed Signed-off-by:ghostjat --- examples/wsc.php | 78 ++++++++++++++++++++++++++++++++++++++++++++ src/Core/Shoonya.php | 62 +++++++++++++++++++++-------------- 2 files changed, 115 insertions(+), 25 deletions(-) create mode 100644 examples/wsc.php diff --git a/examples/wsc.php b/examples/wsc.php new file mode 100644 index 0000000..f5f764b --- /dev/null +++ b/examples/wsc.php @@ -0,0 +1,78 @@ + '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"); +} diff --git a/src/Core/Shoonya.php b/src/Core/Shoonya.php index 90c6d04..1d9643f 100644 --- a/src/Core/Shoonya.php +++ b/src/Core/Shoonya.php @@ -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', @@ -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() { @@ -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); } /** @@ -799,17 +796,15 @@ 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)) { @@ -817,9 +812,15 @@ public function subscribe(array|string $intst, $feedType = self::FeedTouchLine) } 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'; @@ -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 @@ -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; } /** @@ -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(); + } }