连接到NodeJS中的websocket时出现协议错误

2024-09-28 23:36:32 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我已经成功地从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应用程序中运行它,因为我的其余代码都是从这个应用程序运行的,我不希望运行两个不同的服务器


Tags: sendlogjsondatastringsessioncreatechart