|
|
@ -1,23 +1,26 @@ |
|
|
|
class WebSocketClass { |
|
|
|
constructor(url) { |
|
|
|
this.lockReconnect = false; // 是否开始重连
|
|
|
|
this.wsUrl = ""; // ws 地址
|
|
|
|
this.wsUrl = ''; // ws 地址
|
|
|
|
this.globalCallback = null; // 回调方法
|
|
|
|
this.userClose = false; // 是否主动关闭
|
|
|
|
this.createWebSocket(url); |
|
|
|
this.id = null; |
|
|
|
} |
|
|
|
setId(id) { |
|
|
|
this.id = id |
|
|
|
} |
|
|
|
|
|
|
|
createWebSocket(url) { |
|
|
|
// #ifdef H5
|
|
|
|
if (typeof(WebSocket) === 'undefined') { |
|
|
|
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据"); |
|
|
|
this.writeToScreen('您的浏览器不支持WebSocket,无法获取数据'); |
|
|
|
return false |
|
|
|
} |
|
|
|
// #endif
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
if (typeof(uni.connectSocket) === 'undefined') { |
|
|
|
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据"); |
|
|
|
this.writeToScreen('您的浏览器不支持WebSocket,无法获取数据'); |
|
|
|
return false |
|
|
|
} |
|
|
|
// #endif
|
|
|
@ -36,7 +39,7 @@ class WebSocketClass { |
|
|
|
this.ws = uni.connectSocket({ |
|
|
|
url: this.wsUrl, |
|
|
|
success(data) { |
|
|
|
console.log("websocket连接成功"); |
|
|
|
console.log('websocket连接成功'); |
|
|
|
|
|
|
|
that.initEventHandle(); |
|
|
|
}, |
|
|
@ -55,7 +58,7 @@ class WebSocketClass { |
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
this.ws.onopen = (event) => { |
|
|
|
console.log("WebSocket连接打开"); |
|
|
|
console.log('WebSocket连接打开'); |
|
|
|
}; |
|
|
|
// #endif
|
|
|
|
|
|
|
@ -114,7 +117,7 @@ class WebSocketClass { |
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
this.ws.onmessage = (event) => { |
|
|
|
|
|
|
|
|
|
|
|
this.globalCallback(JSON.parse(event.data)) |
|
|
|
}; |
|
|
|
// #endif
|
|
|
@ -138,23 +141,23 @@ class WebSocketClass { |
|
|
|
} |
|
|
|
|
|
|
|
// 发送信息方法
|
|
|
|
webSocketSendMsg(msg) { |
|
|
|
// #ifdef H5
|
|
|
|
this.ws && this.ws.send(JSON.stringify(msg)); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
this.ws && this.ws.send({ |
|
|
|
data: JSON.stringify(msg), |
|
|
|
success() { |
|
|
|
console.log("消息发送成功"); |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.log("关闭失败", err) |
|
|
|
} |
|
|
|
}); |
|
|
|
webSocketSendMsg(msg) { |
|
|
|
// #ifdef H5
|
|
|
|
this.ws && this.ws.send(JSON.stringify(msg)); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
this.ws && this.ws.send({ |
|
|
|
data: JSON.stringify(msg), |
|
|
|
success() { |
|
|
|
console.log('消息发送成功'); |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.log('关闭失败', err) |
|
|
|
} |
|
|
|
}); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 获取ws返回的数据方法
|
|
|
@ -165,27 +168,116 @@ class WebSocketClass { |
|
|
|
// 关闭ws方法
|
|
|
|
closeSocket() { |
|
|
|
if (this.ws) { |
|
|
|
this.userClose = true; |
|
|
|
// #ifdef H5
|
|
|
|
this.ws && this.ws.close(); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
this.ws.close({ |
|
|
|
success(res) { |
|
|
|
console.log("关闭成功", res) |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.log("关闭失败", err) |
|
|
|
} |
|
|
|
}); |
|
|
|
this.userClose = true; |
|
|
|
// #ifdef H5
|
|
|
|
this.ws && this.ws.close(); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
this.ws.close({ |
|
|
|
success(res) { |
|
|
|
console.log('关闭成功', res) |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.log('关闭失败', err) |
|
|
|
} |
|
|
|
}); |
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
writeToScreen(massage) { |
|
|
|
console.log(massage); |
|
|
|
} |
|
|
|
subPairsgroup() { |
|
|
|
|
|
|
|
|
|
|
|
this.publicSend('sub', 'pairsgroup', ['market.pairsgroup'], null) |
|
|
|
} |
|
|
|
unSubPairsgroup() { |
|
|
|
this.publicSend('un_sub', 'pairsgroup', ['market.pairsgroup'], null) |
|
|
|
} |
|
|
|
subBbo(symbol) { |
|
|
|
this.publicSend('sub', 'bbo', ['market.' + symbol + '.bbo'], null) |
|
|
|
} |
|
|
|
unSubBbo(symbol) { |
|
|
|
this.publicSend('un_sub', 'bbo', ['market.' + symbol + '.bbo'], null) |
|
|
|
} |
|
|
|
subTrade(symbol) { |
|
|
|
this.publicSend('sub', 'trade', ['market.' + symbol + '.trade'], null) |
|
|
|
} |
|
|
|
unSubTrade(symbol) { |
|
|
|
this.publicSend('un_sub', 'trade', ['market.' + symbol + '.trade'], null) |
|
|
|
} |
|
|
|
subDetail(symbol) { |
|
|
|
this.publicSend('sub', 'detail', ['market.' + symbol + '.detail'], null) |
|
|
|
} |
|
|
|
unSubDetail(symbol) { |
|
|
|
this.publicSend('un_sub', 'detail', ['market.' + symbol + '.detail'], null) |
|
|
|
} |
|
|
|
subKHistory(timeType, symbol) { |
|
|
|
var toDate = parseInt(new Date().getTime() / 1000); |
|
|
|
var num = 100000; |
|
|
|
var fromDate = parseInt(new Date().getTime() / 1000); |
|
|
|
switch (timeType) { |
|
|
|
case '1min': |
|
|
|
fromDate = fromDate - num; |
|
|
|
break; |
|
|
|
case '5min': |
|
|
|
fromDate = fromDate - (5 * num); |
|
|
|
break; |
|
|
|
case '15min': |
|
|
|
fromDate = fromDate - (15 * num); |
|
|
|
break; |
|
|
|
case '30min': |
|
|
|
fromDate = fromDate - (30 * num); |
|
|
|
break; |
|
|
|
case '60min': |
|
|
|
fromDate = fromDate - (60 * num); |
|
|
|
break; |
|
|
|
case '1day': |
|
|
|
fromDate = fromDate - (60 * 24 * num); |
|
|
|
break; |
|
|
|
case '1week': |
|
|
|
fromDate = fromDate - (60 * 24 * 7 * num); |
|
|
|
break; |
|
|
|
case '1mon': |
|
|
|
fromDate = fromDate - (60 * 24 * 30 * num); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
this.publicSend('req', 'kline', |
|
|
|
['market.' + symbol + ".kline." + timeType], { |
|
|
|
fromDate: parseInt(fromDate), |
|
|
|
toDate: toDate |
|
|
|
}) |
|
|
|
} |
|
|
|
subKline(timeType, symbol) { |
|
|
|
this.publicSend('sub', 'kline', |
|
|
|
['market.' + symbol + ".kline." + timeType], null) |
|
|
|
} |
|
|
|
|
|
|
|
unSubKline(timeType, symbol) { |
|
|
|
this.publicSend('un_sub', 'kline', |
|
|
|
['market.' + symbol + ".kline." + timeType], null) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
publicSend(event, type, channel, date) { |
|
|
|
var data = { |
|
|
|
event: event, |
|
|
|
type: type, |
|
|
|
id: this.id, |
|
|
|
channel: channel |
|
|
|
}; |
|
|
|
if (date) { |
|
|
|
data.fromDate = date.fromDate |
|
|
|
data.toDate = date.toDate |
|
|
|
} |
|
|
|
this.webSocketSendMsg(data) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
export default WebSocketClass; |
|
|
|