|
|
@@ -8,10 +8,12 @@ import { pushMessage50 } from './message'
|
|
|
import { encodeProto, decodeProto } from './package/package50/proto'
|
|
|
|
|
|
export default new (class {
|
|
|
- /** 交易链路 */
|
|
|
- private socket = new MTP2WebSocket(Package50);
|
|
|
-
|
|
|
constructor() {
|
|
|
+ this.socket.onClose = () => {
|
|
|
+ // 停止令牌校验
|
|
|
+ stopCheckToken()
|
|
|
+ }
|
|
|
+
|
|
|
this.socket.onBeforeReconnect = (count) => {
|
|
|
// 停止令牌校验
|
|
|
stopCheckToken()
|
|
|
@@ -21,14 +23,19 @@ export default new (class {
|
|
|
|
|
|
this.socket.onReconnect = () => {
|
|
|
// 重新进行令牌校验
|
|
|
- checkToken().then(() => checkTokenLoop())
|
|
|
- // 交易服务重连成功通知
|
|
|
- eventBus.$emit('TradeServerReconnectSuccessNotify')
|
|
|
+ checkToken().then(() => {
|
|
|
+ checkTokenLoop()
|
|
|
+ // 交易服务重连成功通知
|
|
|
+ eventBus.$emit('TradeServerReconnectSuccessNotify')
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
this.socket.onPush = (pkg) => pushMessage50(pkg, 'encrypted')
|
|
|
}
|
|
|
|
|
|
+ /** 交易链路 */
|
|
|
+ private socket = new MTP2WebSocket(Package50);
|
|
|
+
|
|
|
async connect() {
|
|
|
const res = await service.onReady()
|
|
|
await this.socket.connect(res.tradeUrl)
|
|
|
@@ -39,7 +46,7 @@ export default new (class {
|
|
|
* @param params
|
|
|
* @returns
|
|
|
*/
|
|
|
- send<T>({ data, requestCode, responseCode }: {
|
|
|
+ async send<T>({ data, requestCode, responseCode }: {
|
|
|
data: unknown;
|
|
|
requestCode: keyof typeof FunCode;
|
|
|
responseCode: keyof typeof FunCode;
|
|
|
@@ -47,6 +54,11 @@ export default new (class {
|
|
|
const requestId = FunCode[requestCode]
|
|
|
const responseId = FunCode[responseCode]
|
|
|
|
|
|
+ // 请求之前确保重连后 token 是有效的
|
|
|
+ if (requestCode !== 'TokenCheckReq' && requestCode !== 'LoginReq' && this.socket.connState !== 'Connected') {
|
|
|
+ await checkToken()
|
|
|
+ }
|
|
|
+
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
encodeProto(requestCode, data).then((res) => {
|
|
|
this.connect().then(() => {
|