You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

153 lines
3.4 KiB

import
constant
from '@/utils/constant.js'
import {
Event
} from './event.js'
import pakoJs from './pako.js'
var pako = pakoJs
var socket = {
socket: null, // socket name
id:null,
realTimeData: null, // 请求实时数据的参数
intervalObj: null, // 定时器的名字
lastRealTimeData: null, // 上一次请求的产品
sendData(historyData, realTimeDatas, history) {
// 储存历史数据
this.historyData = historyData
this.realTimeData = realTimeDatas
// 如果上一次订阅过产品
if (this.lastRealTimeData) {
// 请求这一次的历史
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者切换产品咯
// 那么就订阅一下 这次产品的或者周期的 实时数据
if (!history) {
//console.log('订阅新的' + realTimeDatas)
//console.log(111111,realTimeDatas)
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.lastRealTimeData = this.realTimeData
}
} else {
// 如果是第一次订阅,就是说刚进入交易所,
// 先存起来这一次请求的产品 作为历史产品
this.lastRealTimeData = this.realTimeData
// 然后 初始化一下websocket
this.initWs()
}
},
initWs() {
this.socket = new WebSocket(constant.WSSURL)
this.socket.onopen = () => {
}
this.socket.onmessage = resp => {
this.message(resp)
}
this.socket.onclose = () => {
this.close()
}
this.socket.onerror = err => {
this.error(err)
}
},
error(err) {
//console.log(err, 'depth-socket::error')
},
close() {
// 如果websocket关闭的话,就从新打开一下。
this.initWs()
//console.log('depth-socket::close')
},
message(resp) {
resp=JSON.parse(resp.data)
if (resp.channel === 'conn') {
this.id = resp.data
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
}else {
if(resp.event === 'req'){
//Event.emit('data', resp.data)
if (resp.data && Array.isArray(resp.data)) {
Event.emit('data', resp.data)
}
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.sendWsRequest({
event: 'sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
}else{
Event.emit('realTime', resp.data)
}
}
},
checkSendMessage(options) {
// 这里处理websocket 连接不上的问题
var checkTimes = 10
var i = 0
this.intervalObj = setInterval(() => {
i += 1
if (this.socket.readyState === 1) {
// ...
this.socket.send(options)
clearInterval(this.intervalObj)
return
}
if (i >= checkTimes) {
clearInterval(this.intervalObj)
//console.log('send post_data_str timeout.')
}
}, 500)
},
sendWsRequest(options) {
switch (this.socket.readyState) {
case 0:
this.checkSendMessage(JSON.stringify(options))
break
case 1:
this.socket.send(JSON.stringify(options))
break
case 2:
//console.log('ws关闭状态')
break
case 3:
this.initWs()
break
default:
//console.log('ws未知错误')
}
}
}
exports.socket = socket