|
|
@@ -1,15 +1,14 @@
|
|
|
import { v4 } from 'uuid'
|
|
|
-import { useLoginStore } from '@/stores'
|
|
|
+import { FunCode } from '@/constants/funcode'
|
|
|
import service from '@/services'
|
|
|
import eventBus from '@/services/bus'
|
|
|
-import { quoteSubscribe } from '@/services/api/quote'
|
|
|
import { MTP2WebSocket } from './build'
|
|
|
import { Package40 } from './package'
|
|
|
-import { parseReceivePush } from './package/package40/decode'
|
|
|
+import { parseReceivePush, parseSubscribeRsp } from './package/package40/decode'
|
|
|
+import { subscribeListToByteArrary } from './package/package40/encode'
|
|
|
+import Long from 'long'
|
|
|
|
|
|
export default new (class {
|
|
|
- private loginStore = useLoginStore()
|
|
|
-
|
|
|
/** 行情链路 */
|
|
|
private socket = new MTP2WebSocket(Package40)
|
|
|
|
|
|
@@ -38,37 +37,51 @@ export default new (class {
|
|
|
|
|
|
async connect() {
|
|
|
const res = await service.onReady()
|
|
|
- await this.socket.connect(res.quoteWS, this.loginStore.token)
|
|
|
+ await this.socket.connect(res.quoteUrl)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 开始行情订阅
|
|
|
*/
|
|
|
private subscribe = () => {
|
|
|
- const quoteGoodses: Model.QuoteSubscribeReq['quoteGoodses'] = []
|
|
|
+ const goodsList: Proto.QuoteReq[] = []
|
|
|
|
|
|
this.subscribeMap.forEach((list) => {
|
|
|
list.forEach((code) => {
|
|
|
- if (!quoteGoodses.some((e) => e.goodsCode === code)) {
|
|
|
- quoteGoodses.push({
|
|
|
- exchangeId: 250,
|
|
|
+ if (!goodsList.some((e) => e.goodsCode === code)) {
|
|
|
+ goodsList.push({
|
|
|
+ exchangeCode: 250,
|
|
|
goodsCode: code,
|
|
|
+ subState: 0,
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- if (quoteGoodses.length) {
|
|
|
- console.log('开始行情订阅', quoteGoodses)
|
|
|
+ if (goodsList.length) {
|
|
|
+ console.log('开始行情订阅', goodsList)
|
|
|
this.connect().then(() => {
|
|
|
- quoteSubscribe({
|
|
|
+ const content = subscribeListToByteArrary(goodsList, '2_TOKEN_NEKOT_', Long.fromNumber(2))
|
|
|
+
|
|
|
+ this.socket.send({
|
|
|
data: {
|
|
|
- quoteGoodses
|
|
|
+ rspCode: FunCode.QuoteSubscribeRsp,
|
|
|
+ payload: new Package40(FunCode.QuoteSubscribeReq, content)
|
|
|
+ },
|
|
|
+ success: (raw) => {
|
|
|
+ if (raw.content) {
|
|
|
+ parseSubscribeRsp(raw.content).then((res) => {
|
|
|
+ console.log('行情订阅成功', res)
|
|
|
+ }).catch(() => {
|
|
|
+ console.error('报文解析失败', raw)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ console.error('数据异常')
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('行情订阅失败', err)
|
|
|
}
|
|
|
- }).then((res) => {
|
|
|
- console.log('行情订阅成功', res)
|
|
|
- }).catch((err) => {
|
|
|
- console.error('行情订阅失败', err)
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
@@ -81,20 +94,17 @@ export default new (class {
|
|
|
* @returns
|
|
|
*/
|
|
|
createSubscribe = (key?: string) => {
|
|
|
- const loginStore = useLoginStore()
|
|
|
const uuid = key ?? v4()
|
|
|
const values = this.subscribeMap.get(uuid) ?? []
|
|
|
|
|
|
const start = (...goodsCodes: string[]) => {
|
|
|
- if (loginStore.token) {
|
|
|
- goodsCodes.forEach((code) => {
|
|
|
- if (code && !values.includes(code)) {
|
|
|
- values.push(code)
|
|
|
- }
|
|
|
- })
|
|
|
- this.subscribeMap.set(uuid, values)
|
|
|
- this.subscribe()
|
|
|
- }
|
|
|
+ goodsCodes.forEach((code) => {
|
|
|
+ if (code && !values.includes(code)) {
|
|
|
+ values.push(code)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ this.subscribeMap.set(uuid, values)
|
|
|
+ this.subscribe()
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
@@ -105,9 +115,7 @@ export default new (class {
|
|
|
if (flag) {
|
|
|
console.log('删除订阅', uuid)
|
|
|
}
|
|
|
- if (loginStore.token) {
|
|
|
- this.subscribe()
|
|
|
- }
|
|
|
+ this.subscribe()
|
|
|
return flag
|
|
|
},
|
|
|
}
|