123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- const wssUrl = "wss://api.hadax.com/ws"
- const pako = window.pako
- const unitMap = {
- m: 'min',
- h: 'hour',
- d: 'day',
- w: 'week',
- M: 'mon',
- }
- const chart = klinecharts.init('chart_container')
- let chart_type=0;
- function loading(type){
- console.info(type)
- if(type==0){
- $('.loding').css('display','flex');
- $('.loding').data('type','1');
- }else{
- $('.loding').css('display','none');
- $('.loding').data('type','0');
- }
- }
- function changInterval(interval,th){
- loading(0)
- console.info(VyKline)
- window.deedfeeds.intervalChanged({interval:interval,setHistoryData: VyKline.initHistoryData,subscribeData: VyKline.getRealTimeData})
- console.info(th)
- $(th).addClass('active');
- $(th).siblings().removeClass('active')
-
-
- }
- function initOnReady1(kLineDataList){
-
- chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' })
-
- chart.createTechnicalIndicator('VOL')
-
-
-
- kLineDataList.reverse()
-
- var chartDataList = kLineDataList.map(function (data) {
- return {
- timestamp: data.time,
- open: data.open,
- high: data.high,
- low: data.low,
- close: data.close,
- volume:data.volume,
- }
- })
-
- chart.applyNewData(chartDataList)
- chart.setOffsetRightSpace(2)
- chart_type=1;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- function formatPeriod(str) {
- const reg = /([A-z]+)([0-9]+)/
- const matchedArr = str.match(reg)
- if (matchedArr) {
- return matchedArr[2] + unitMap[matchedArr[1]]
- } else {
- console.error("格式化时间粒度出错!");
- }
- }
- const SYMBOL =window.SYMBOL;
- const dataNum = 900
- const historyK = (interval) => {
-
- const reg1 = /([A-z]+)([0-9]+)/
- let matchedArr = interval.match(reg1)
- const reg = /[0-9]+/
- let shijian = interval.match(reg)[0]
- if(matchedArr[1] == 'h'){
- shijian = matchedArr[2] * 60
- }
- if(matchedArr[1] == 'd'){
- shijian = matchedArr[2] * 60*24
- }
- if(matchedArr[1] == 'w'){
- shijian = matchedArr[2] * 60*24*3.5
- }
- let form1 = Math.trunc(Date.now() / 1000) - 60 * dataNum * shijian
-
-
-
-
-
-
-
-
-
- let str = {
- req: `market.${SYMBOL}.kline.${formatPeriod(interval)}`,
- id: SYMBOL,
- from:form1,
- to: Math.trunc(Date.now() / 1000)
- }
- return str
- }
- const subK = (interval) => ({
- sub: `market.${SYMBOL}.kline.${formatPeriod(interval)}`,
- id: SYMBOL
- })
- const unsubK = (interval) => ({
- unsub: `market.${SYMBOL}.kline.${formatPeriod(interval)}`,
- id: SYMBOL
- })
- class Deedfeeds {
- getMarketSpecialtyJsonAitclast(interval){
- const reg1 = /([A-z]+)([0-9]+)/
- let matchedArr = interval.match(reg1)
-
- let shijian = matchedArr[2]
- if(matchedArr[1] == 'h'){
- shijian = matchedArr[2] * 60
- }
- if(matchedArr[1] == 'd'){
- shijian = matchedArr[2] * 60*24
- }
- if(matchedArr[1] == 'w'){
- shijian = matchedArr[2] * 60*24*3.5
- }
- let _this= this
-
- $.ajax({
-
- type : "GET",
-
- contentType: "application/json;charset=UTF-8",
-
- url : "/Home/Chart/getMarketSpecialtyJsonAitclast.html?step="+shijian*60,
-
- success : function(result) {
- var historyData = []
- console.log(result)
- var item = result[0]
- console.log(historyData)
- _this.subscribeDataParam.subscribeData({
- time: parseInt(item.time) * 1000,
- open: parseFloat(item.open),
- high: parseFloat(item.high),
- low: parseFloat(item.low),
- close: parseFloat(item.close),
- volume: parseFloat(item.volume)
- })
- chart.updateData(
- {
-
- open: parseFloat(item.open),
-
- close: parseFloat(item.close),
-
- high: parseFloat(item.high),
-
- low: parseFloat(item.low),
-
- volume: parseFloat(item.volume),
-
- timestamp: parseInt(item.time) * 1000
- }
- );
-
-
-
-
-
- localStorage.setItem('closeshijian'+SYMBOL,item.close)
-
- },
-
- error : function(e){
- console.log(e.status);
- console.log(e.responseText);
- }
- });
- }
-
- ajaxaitc(interval){
-
- let _this= this
- const reg1 = /([A-z]+)([0-9]+)/
- let matchedArr = interval.match(reg1)
- let shijian = matchedArr[2]
- if(matchedArr[1] == 'h'){
- shijian = matchedArr[2] * 60
- }
- if(matchedArr[1] == 'd'){
- shijian = matchedArr[2] * 60*24
- }
- if(matchedArr[1] == 'w'){
- shijian = matchedArr[2] * 60*24*3.5
- }
-
- $.ajax({
-
- type : "GET",
-
- contentType: "application/json;charset=UTF-8",
-
- url : "/Home/Chart/getMarketSpecialtyJsonAitc.html?market=usdz_usdt&step="+shijian*60,
-
- success : function(result) {
- console.info(result)
-
- var historyData = []
- result && result.forEach(item => {
- historyData.unshift({
- time: parseInt(item.time) * 1000,
- open: parseFloat(item.open),
- high: parseFloat(item.high),
- low: parseFloat(item.low),
- close: parseFloat(item.close),
- volume: parseFloat(item.volume)
- })
- })
-
- _this.subscribeDataParam.setHistoryData(historyData)
- if(matchedArr[0] == 'm1'){
- localStorage.setItem('closeshijian'+SYMBOL,historyData[0].close)
- }else{
- localStorage.removeItem('closeshijian'+SYMBOL)
- }
- console.info(chart_type)
- if(chart_type==0){
- loading(1)
- chart.setPriceVolumePrecision(6,6)
- initOnReady1(historyData)
- }else{
- let historyData1=[]
- result && result.forEach(item => {
- historyData1.unshift({
- timestamp: parseInt(item.time) * 1000,
- open: parseFloat(item.open),
- high: parseFloat(item.high),
- low: parseFloat(item.low),
- close: parseFloat(item.close),
- volume: parseFloat(item.volume)
- })
- })
- loading(1)
- chart.applyNewData(historyData1.reverse())
- }
-
-
- },
-
- error : function(e){
- console.log(e.status);
- console.log(e.responseText);
- }
- });
- }
- constructor() {
- this.ws = new WebSocket(wssUrl)
- this.currentInterval = null
- this.subscribeDataParam = {
- interval: null,
- setHistoryData: null,
- subscribeData: null
- }
- }
- handleData(msg) {
-
- if (!this.subscribeDataParam.interval) {
- console.error("订阅数据参数错误!");
- return
- }
-
- let data = JSON.parse(msg)
-
-
- if (data.ping) {
-
-
-
-
- this.ws.send(JSON.stringify({ pong: data.ping }));
- } else if (data.status === "ok") {
-
-
-
- if (data.data) {
- const historyData = []
-
- data.data && data.data.forEach(item => {
- historyData.unshift({
- time: item.id * 1000,
- open: item.open,
- high: item.high,
- low: item.low,
- close: item.close,
- volume: item.vol
- })
- })
- console.log(historyData)
- loading(1)
- if(chart_type==0){
- chart.setPriceVolumePrecision(2,2)
- initOnReady1(historyData)
-
- }else{
- let historyData1=[]
- data.data && data.data.forEach(item => {
- historyData1.unshift({
- timestamp: item.id * 1000,
- open: item.open,
- high: item.high,
- low: item.low,
- close: item.close,
- volume: item.vol
- })
- })
- historyData1.reverse()
- chart.applyNewData(historyData1)
- }
-
- this.subscribeDataParam.setHistoryData(historyData)
- } else if (data.unsubbed) {
- this.ws.send(JSON.stringify(historyK(this.subscribeDataParam.interval)));
- this.ws.send(JSON.stringify(subK(this.subscribeDataParam.interval)));
- }
- } else {
-
- console.log(data);
-
- if (data.tick) {
- const perData = data.tick
- const perData1={
-
- open: perData.open,
-
- close: perData.close,
-
- high: perData.high,
-
- low: perData.low,
-
- volume: perData.vol,
-
-
- timestamp:perData.id * 1000,
- }
- console.info(perData1)
- chart.updateData(perData1);
- localStorage.setItem('closeshijian'+SYMBOL,perData.close)
- this.subscribeDataParam.subscribeData({
- time: perData.id * 1000,
- open: perData.open,
- high: perData.high,
- low: perData.low,
- close: perData.close,
- volume: perData.vol
- })
-
- }
- }
- }
-
-
- setHistoryData({ interval, setHistoryData, subscribeData }) {
-
-
- this.currentInterval = interval
- this.ws.onopen = () => {
-
- if(SYMBOL == 'usdzusdt'){
-
- this.ajaxaitc(interval)
-
- let _this = this
-
- window.a = setInterval(function(){
-
- _this.getMarketSpecialtyJsonAitclast(interval)
- }, 2000);
- }else{
- this.ws.send(JSON.stringify(historyK(interval)));
- this.ws.send(JSON.stringify(subK(interval)));
- }
- }
- this.subscribeDataParam = {
- interval,
- setHistoryData,
- subscribeData
- }
- this.ws.onmessage = event => {
- let blob = event.data;
- const fileReader = new FileReader();
- fileReader.onload = e => {
- let ploydata = new Uint8Array(e.target.result);
- let msg = pako.inflate(ploydata, { to: 'string' });
-
- this.handleData(msg);
- };
- fileReader.readAsArrayBuffer(blob, "utf-8")
- }
- }
- intervalChanged({ interval, setHistoryData, subscribeData }) {
- this.subscribeDataParam = {
- interval,
- setHistoryData,
- subscribeData
- }
- console.log('重新请求')
- if(SYMBOL == 'usdzusdt'){
-
- clearInterval(window.a);
- clearInterval(window.b);
- let _this=this
- window.b= setInterval(function (){
- _this.getMarketSpecialtyJsonAitclast(interval)
- },2000)
- this.ajaxaitc(interval)
- }else{
- this.ws.send(JSON.stringify(unsubK(this.currentInterval)))
- }
-
- this.currentInterval = interval
- }
- }
- window.deedfeeds = new Deedfeeds();
|