所以我已经成功地从 python 中的一个开源目录运行了这段代码,但是我试图将它转换为我的 NodeJS 应用程序的 JavaScript,并且当我尝试运行代码时遇到以下输出问题。
session generated qs_pycievcoeqqk
chart_session generated cs_vdapetsoyiha
TradingView connection established
~m~330~m~{"session_id":"<0.21600.2149>_chi1-charts-10-webchart-8@chi1-compute-
10_x","timestamp":1610696059,"release":"registry:5000/tvbs_release/webchart:release_203-141","studies_metadata_hash":"7f415d69d109bc526bcff871502a9174924f1441","protocol":"json","javastudies":"javastudies-3.60_1268","auth_scheme_vsn":2,"via":"92.223.69.22:443"}
~m~41~m~{"m":"protocol_error","p":["wrong data"]}
TradingView connection closed
这是我的代码:标头 JSON
{
"Connection": "upgrade",
"Host": "data.tradingview.com",
"Origin": "https://data.tradingview.com",
"Cache-Control": "no-cache",
"Sec-WebSocket-Protocol": "json",
"Upgrade": "websocket",
"User-Agent": "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.56",
"Pragma": "no-cache",
"Upgrade": "websocket"
}
其余代码:
const ws = require('ws');
const alpha = require('alphabet-generator');
let headers = require('./tradingiew.json');
function generateChartSession() {
let string = alpha.random(12).get().join('');
string = 'cs_' + string;
return string.toLowerCase();
}
function generateSession() {
let string = alpha.random(12).get().join('');
string = 'qs_' + string;
return string.toLowerCase();
}
const session = generateSession();
console.log('session generated', session);
const chart_session = generateChartSession();
console.log('chart_session generated', chart_session);
let messages = {
set_auth_token: 'unauthorized_user_token',
chart_create_session: [ chart_session, "" ],
quote_create_session: [ session ],
quote_set_fields: [session,"ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","rchp","rtc"],
quote_add_symbols: [session, "CME_MINI:ESH2021", {"flags":['force_permission']}],
resolve_symbol: [chart_session, "symbol_1","={\"symbol\":\"CME_MINI:ESH2021\",\"adjustment\":\"splits\"}"],
create_series: [chart_session,"s1","s1","symbol_1","1",300],
quote_fast_symbols: [session,"CME_MINI:ESH2021"],
create_study: [chart_session,"st1","st1","s1","Volume@tv-basicstudies-118",{"length":20,"col_prev_close":"false"}],
quote_hibernate_all: [session]
};
let tvWS = new ws(
'wss://data.tradingview.com/socket.io/websocket', { headers }
);
tvWS.on('open', async function() {
console.log('TradingView connection established');
tvWS.send(JSON.stringify(messages.set_auth_token));
tvWS.send(JSON.stringify(messages.chart_create_session));
tvWS.send(JSON.stringify(messages.quote_create_session));
tvWS.send(JSON.stringify(messages.quote_set_fields));
tvWS.send(JSON.stringify(messages.quote_add_symbols));
tvWS.send(JSON.stringify(messages.resolve_symbol));
tvWS.send(JSON.stringify(messages.create_series));
tvWS.send(JSON.stringify(messages.quote_fast_symbols));
tvWS.send(JSON.stringify(messages.create_study));
tvWS.send(JSON.stringify(messages.quote_hibernate_all));
});
tvWS.on('error', async function(error) {
console.log('TradingView error:', error);
});
tvWS.on('close', async function(e) {
console.log('TradingView connection closed');
});
tvWS.on('message', async function(data) {
console.log(data);
});
所以正如我所说,我让它在 python 中成功运行,但我更愿意在我的 NodeJS 应用程序中运行它,因为这是我的其余代码运行的地方,我不想运行 2 个不同的服务器。