【发布时间】:2021-01-15 17:49:41
【问题描述】:
所以我已经成功地从 python 中的一个开源目录运行 this code,但我正在尝试将其转换为我的 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 个不同的服务器。
【问题讨论】:
-
使用
ws库,您无需手动设置特定于 websocket 的标头 - 它会为您完成。我的猜测是您试图放入标头中的一些特定于 webSocket 的东西会搞砸。把所有这些都拿出来。 -
没有标头就无法连接我得到 403 回复
-
您是否尝试与 socket.io 服务器建立 webSocket 连接?如果是这样,你不能那样做。 socket.io 服务器只接受来自 socket.io 客户端的连接。我看到网址是
wss://data.tradingview.com/socket.io/websocket。那肯定看起来像一个 socket.io 端点。 -
好的,我试试,谢谢你的帮助
-
我应该提到,对于socket.io,您还需要相同版本的客户端和服务器才能连接。这种方式是出了名的挑剔。
标签: python node.js websocket http-protocols