1

我正在尝试将 tradingview 图表小部件用于我自己的数据集,但我对如何通过 websocket 使其工作有点困惑(我发现的所有示例都使用 REST)。我的困惑在于“binanceSymbols()”方法。该示例显示了一个 REST 调用,但我想通过打开的 websocket 连接请求符号并解析响应。在执行 websocket 请求并等待响应时,fetch/then 的等价物是什么?

我正在使用https://github.com/bergusman/tradingview-jsapi-binance作为参考来尝试弄清楚。

HTML:

<body>
 <div id="tv_chart_container" class="chart_panel"></div>
</body>

JS:

class DataFeed
{
console.log("Datafeed:Loaded");
    this.binanceHost='http://localhost:8080'
    this.debug =options.debug ||false

    onReady(callback) {
        this.binanceSymbols().then((symbols) => {
            this.symbols = symbols
            callback({
                supports_marks: false,
                supports_timescale_marks: false,
                supports_time: true,
                supported_resolutions: [
                    '1', '3', '5', '15', '30', '60', '120', '240', '360', '480', '720', '1D', '3D', '1W', '1M'
                ]
            })
        }).catch(err => {
            console.error(err)
        })
    }

  binanceSymbols() {

        return fetch(this.binanceHost + '/api/v1/exchangeInfo').then(res => {
            return res.json()
        }).then(json => {
            return json.symbols
        })
    }


}

var websocket = new WebSocket('ws://localhost:80080');
websocket.onmessage = function processMsg(evt)
{
var json = evt.data;
responseHandler(json);
}

function responseHandler(inmsg)
{
inmsg = JSON.parse(inmsg);
if(inmsg.response[0].event=="login_success")
{
LoadChart();
}
function LoadChart()
{


            TradingView.onready(function()
            {
            var config = {
            debug:true,
            exchanges:[],
            symbols_types:[],
            supported_resolutions:[],
            supports_marks:false,
            supports_timescale_marks:false,
            supports_time:false,
            futures_regex:'/^(.+)([12]!|[FGHJKMNQUVXZ]\d{1,2})$/',

            }

            var _widget = window.tvWidget = new TradingView.widget({
                  symbol: 'AAPL',
                  fullscreen:true,
                  interval: '15',
                  container_id: "tv_chart_container",
                  datafeed: new Datafeed(config),
                  library_path: "charting_library/",
                  locale: getParameterByName('lang') || "en",
                  disabled_features: ["use_localstorage_for_settings", "header_symbol_search", "symbol_search_hot_key"],
                  debug: false,
                  loading_screen: { backgroundColor: "#000000", foregroundColor:"#29e2bd" },
                  overrides: {
                    "paneProperties.background": "#222222",
                    "paneProperties.vertGridProperties.color": "#454545",
                    "paneProperties.horzGridProperties.color": "#454545",
                    "symbolWatermarkProperties.transparency": 90,
                    "scalesProperties.textColor": "#AAA"
                  }
                });


            });
}

}
4

0 回答 0