Skip to content

Commit 872d893

Browse files
committed
added webScoket feed
Signed-off-by:ghostjat <[email protected]>
1 parent 793f723 commit 872d893

File tree

2 files changed

+115
-25
lines changed

2 files changed

+115
-25
lines changed

examples/wsc.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use Core\Shoonya;
6+
7+
$loop = React\EventLoop\Loop::get();
8+
$hs = json_encode(['t' => 'h']);
9+
10+
$api = new Shoonya();
11+
if ($api->login()) {
12+
echo 'Loggedin ....!' . PHP_EOL;
13+
echo 'attemping to connect the webscoket server...' . PHP_EOL;
14+
15+
\Ratchet\Client\connect('wss://api.shoonya.com/NorenWSTP/')->then(function ($conn) use ($loop, $hs, $api) {
16+
$loop->addPeriodicTimer(3000, function () use ($conn, $hs) {
17+
$conn->send($hs);
18+
});
19+
20+
$conn->send($api->connectWS());
21+
22+
$conn->on('message', function ($msg) use ($conn, $api) {
23+
$res = json_decode($msg);
24+
echo "Received: {$res->t}\n";
25+
switch ($res->t) {
26+
case 'ck':
27+
echo 'Conected TO webSocketServer' . PHP_EOL;
28+
$conn->send($api->subscribe('BSE|543257',$api::FeedSnapQuoate));
29+
break;
30+
case 'tk' || 'tf':
31+
echo 'touchline task' . PHP_EOL;
32+
cls();
33+
print_r($res);
34+
break;
35+
case 'dk' || 'df':
36+
file_put_contents('irfc.json', $msg, FILE_APPEND);
37+
system('clear');
38+
print_r($res);
39+
break;
40+
case 'om':
41+
break;
42+
default:
43+
$conn->send($api->unsubscribe('BSE|543257',$api::FeedSnapQuoate));
44+
$conn->close();
45+
break;
46+
}
47+
});
48+
}, function ($e) use ($api) {
49+
echo "Could not connect: {$e->getMessage()}\n";
50+
echo ($api->loadChart()) ? 'logout succesfully' . PHP_EOL : 'failed to logout' . PHP_EOL;
51+
});
52+
#$loop->run();
53+
}
54+
55+
function trigger($data) {
56+
foreach($data as $single) {
57+
$implode[] = implode(', ', $single);
58+
}
59+
echo implode(', ', $implode) .PHP_EOL;
60+
}
61+
62+
function rcvQuotes($data) {
63+
64+
}
65+
66+
function rcvOrders($data) {
67+
68+
}
69+
70+
function open($data) {
71+
global $api;
72+
$instruments = 'NSE|22#BSE|500400';
73+
$api->subscribe($instruments);
74+
}
75+
76+
function cls() {
77+
print("\033[2J\033[;H");
78+
}

src/Core/Shoonya.php

+37-25
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ class Shoonya {
2222
public const FeedTouchLine = 't', FeedSnapQuoate = 'd';
2323
public const PriceMarket = 'MKT', PriceLimit = 'LMT', PrinceSLLmit = 'SL-LMT', PriceSLM = 'SL-MKT', DS = 'DS', L2 = '2L', L3 = '3L';
2424
public const Buy = 'B', Sell = 'S', AITG = 'LTP_A_O', AITL = 'LTP_B_O';
25-
public const CIQ = 'lib/chartIQ/FCIQ/index.php?', TVC = 'lib/tvcl/?';
26-
25+
public const CIQ = 'lib/chartIQ/FCIQ/index.php?', TVC = 'NorenCharts/?';
2726
public $tmp;
2827
protected $urls = [
2928
'host' => 'https://api.shoonya.com/',
3029
'ws_endpoint' => 'wss://api.shoonya.com/NorenWSTP',
3130
'endpoint' => 'https://api.shoonya.com/NorenWClientTP',
32-
'eodhost' => 'https://api.shoonya.com/chartApi/getdata',
33-
'webchart' => 'https://trade.shoonya.com/',
34-
'localchart' => 'http://127.0.0.1:8080/'
31+
'webchart' =>'https://trade.shoonya.com/',
32+
'localchart' =>'http://127.0.0.1:8080/'
3533
];
3634
protected $routes = [
3735
'login' => '/QuickAuth',
@@ -67,7 +65,10 @@ class Shoonya {
6765
'cancelalert' => '/CancelAlert',
6866
'modifyalert' => '/ModifyAlert',
6967
'getpendingalert' => '/GetPendingAlert',
70-
'getenabledalert' => '/GetEnabledAlertTypes'
68+
'getenabledalert' => '/GetEnabledAlertTypes',
69+
'spanCalculator' =>'/SpanCalc',
70+
'optionGreek' =>'/GetOptionGreek',
71+
'DPSeries' =>'/EODChartData'
7172
];
7273

7374
public function __construct() {
@@ -411,11 +412,7 @@ public function getDailyPriceSeries(string $tysm, string $startDate, string $end
411412
'to' => $et
412413
];
413414

414-
$request = $this->guzzle->post($this->urls['eodhost'], [
415-
'header' => ['Content-Type' => 'application/json'],
416-
'body' => json_encode($values)
417-
]);
418-
return $this->decode($request->getBody());
415+
return $this->request('DPSeries', $values);
419416
}
420417

421418
/**
@@ -799,27 +796,31 @@ public function connectWS() {
799796
'actid' => $this->accountId,
800797
'ordersource' => 'API',
801798
'susertoken' => $this->jKey];
802-
$this->wsC = new \WSSC\WebSocketClient($this->urls['ws_endpoint'], new \WSSC\Components\ClientConfig());
803-
$this->wsC->send(json_encode($value));
804-
print_r($this->wsC->receive());
805-
if ($this->wsC->isConnected()) {
806-
return true;
799+
return json_encode($value);
807800
}
808-
echo 'Failed to connect to WSS' . PHP_EOL;
809-
return false;
810-
}
811801

812-
public function subscribe(array|string $intst, $feedType = self::FeedTouchLine) {
802+
/**
803+
* Subscribe to quotes feeds (websocket)
804+
* @param array|string $intst
805+
* @param type $feedType
806+
*/
807+
public function subscribeFeed(array|string $intst, $feedType = self::FeedTouchLine) {
813808
$values = [];
814809
$values['t'] = $feedType;
815810
if (is_array($intst)) {
816811
$values['k'] = implode('#', $intst);
817812
} else {
818813
$values['k'] = $intst;
819814
}
815+
return json_encode($values);
820816
}
821817

822-
public function unsubscribe(array|string $intst, $feedType = self::FeedTouchLine) {
818+
/**
819+
* Unsubscribe to quotes feed(websocket)
820+
* @param array|string $intst
821+
* @param type $feedType
822+
*/
823+
public function unsubscribeFeed(array|string $intst, $feedType = self::FeedTouchLine) {
823824
$values = [];
824825
if ($feedType == self::FeedTouchLine) {
825826
$values['t'] = 'u';
@@ -835,12 +836,20 @@ public function unsubscribe(array|string $intst, $feedType = self::FeedTouchLine
835836
}
836837

837838
/**
838-
* @todo
839+
* subscribe to orders feed (websocket)
840+
* @return type
839841
*/
840-
public function subscribeOrders() {
841-
$values = ['t' => 'o', 'actid' => $this->accountId];
842+
public function subscribeOrdersFeed() {
843+
return json_encode(['t' => 'o', 'actid' => $this->accountId]);
842844
}
843845

846+
/**
847+
* unsubscribe orders feed(websocket)
848+
*/
849+
public function unSubscibeOrdersFeed() {
850+
return json_encode(['t'=>'uo','t'=>'uok']);
851+
}
852+
844853
/**
845854
* Set Alert
846855
* @param string $tsym
@@ -930,7 +939,7 @@ public function loadChart(string $tsym = 'nifty 50', string $exch = 'NSE', strin
930939
($tsym == 'NIFTY50' || $tsym == 'nifty50') ? $tsym = 'NIFTY_50' : $tsym . '-EQ';
931940
$encode = base64_encode('user=' . $this->uid . '&token=' . $this->jKey . "&exch_tsym=NSE:$tsym:$tsym&p=Web");
932941
}
933-
return $this->urls['localchart'] . $chartType . $encode;
942+
return ($chartType == self::TVC) ? $this->urls['webchart'].$chartType.$encode:$this->urls['localchart'].$chartType.$encode;
934943
}
935944

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

1036+
public function __destruct() {
1037+
$this->logout();
1038+
}
10271039
}

0 commit comments

Comments
 (0)