From 7ebadf707040b4bc8bc85cc23626995140d8e75a Mon Sep 17 00:00:00 2001 From: tangmingyou Date: Tue, 28 Mar 2023 18:02:19 +0800 Subject: [PATCH] =?UTF-8?q?header=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/dev.js | 4 +- src/api/proto.js | 60 +++++++++++++++++++++++++ src/api/websocket.js | 73 ++++++++++++++++++++----------- src/components/title.jsx | 19 ++++++-- src/pages/new_table/new_table.jsx | 2 +- src/store/conn.js | 2 +- 6 files changed, 128 insertions(+), 32 deletions(-) diff --git a/config/dev.js b/config/dev.js index 6b96c44..6eda426 100644 --- a/config/dev.js +++ b/config/dev.js @@ -10,8 +10,8 @@ module.exports = { h5: { devServer: { //host: '0.0.0.0', - // host: '192.168.110.53', - host: '192.168.3.101', + host: '192.168.110.53', + // host: '192.168.3.101', port: 10086, // 设置代理来解决 H5 请求的跨域问题 proxy: { diff --git a/src/api/proto.js b/src/api/proto.js index 4ab3246..3c9a4c7 100644 --- a/src/api/proto.js +++ b/src/api/proto.js @@ -6028,6 +6028,8 @@ export const api = $root.api = (() => { * @interface IBetRole * @property {number|null} [betMin] BetRole betMin * @property {number|null} [betMax] BetRole betMax + * @property {number|Long|null} [betTimeLimit] BetRole betTimeLimit + * @property {number|null} [betSecondLimit] BetRole betSecondLimit * @property {Array.|null} [betOpts] BetRole betOpts */ @@ -6063,6 +6065,22 @@ export const api = $root.api = (() => { */ BetRole.prototype.betMax = 0; + /** + * BetRole betTimeLimit. + * @member {number|Long} betTimeLimit + * @memberof api.BetRole + * @instance + */ + BetRole.prototype.betTimeLimit = $util.Long ? $util.Long.fromBits(0,0,false) : 0; + + /** + * BetRole betSecondLimit. + * @member {number} betSecondLimit + * @memberof api.BetRole + * @instance + */ + BetRole.prototype.betSecondLimit = 0; + /** * BetRole betOpts. * @member {Array.} betOpts @@ -6099,6 +6117,10 @@ export const api = $root.api = (() => { writer.uint32(/* id 1, wireType 0 =*/8).int32(message.betMin); if (message.betMax != null && Object.hasOwnProperty.call(message, "betMax")) writer.uint32(/* id 2, wireType 0 =*/16).int32(message.betMax); + if (message.betTimeLimit != null && Object.hasOwnProperty.call(message, "betTimeLimit")) + writer.uint32(/* id 9, wireType 0 =*/72).int64(message.betTimeLimit); + if (message.betSecondLimit != null && Object.hasOwnProperty.call(message, "betSecondLimit")) + writer.uint32(/* id 10, wireType 0 =*/80).int32(message.betSecondLimit); if (message.betOpts != null && message.betOpts.length) { writer.uint32(/* id 13, wireType 2 =*/106).fork(); for (let i = 0; i < message.betOpts.length; ++i) @@ -6147,6 +6169,14 @@ export const api = $root.api = (() => { message.betMax = reader.int32(); break; } + case 9: { + message.betTimeLimit = reader.int64(); + break; + } + case 10: { + message.betSecondLimit = reader.int32(); + break; + } case 13: { if (!(message.betOpts && message.betOpts.length)) message.betOpts = []; @@ -6199,6 +6229,12 @@ export const api = $root.api = (() => { if (message.betMax != null && message.hasOwnProperty("betMax")) if (!$util.isInteger(message.betMax)) return "betMax: integer expected"; + if (message.betTimeLimit != null && message.hasOwnProperty("betTimeLimit")) + if (!$util.isInteger(message.betTimeLimit) && !(message.betTimeLimit && $util.isInteger(message.betTimeLimit.low) && $util.isInteger(message.betTimeLimit.high))) + return "betTimeLimit: integer|Long expected"; + if (message.betSecondLimit != null && message.hasOwnProperty("betSecondLimit")) + if (!$util.isInteger(message.betSecondLimit)) + return "betSecondLimit: integer expected"; if (message.betOpts != null && message.hasOwnProperty("betOpts")) { if (!Array.isArray(message.betOpts)) return "betOpts: array expected"; @@ -6225,6 +6261,17 @@ export const api = $root.api = (() => { message.betMin = object.betMin | 0; if (object.betMax != null) message.betMax = object.betMax | 0; + if (object.betTimeLimit != null) + if ($util.Long) + (message.betTimeLimit = $util.Long.fromValue(object.betTimeLimit)).unsigned = false; + else if (typeof object.betTimeLimit === "string") + message.betTimeLimit = parseInt(object.betTimeLimit, 10); + else if (typeof object.betTimeLimit === "number") + message.betTimeLimit = object.betTimeLimit; + else if (typeof object.betTimeLimit === "object") + message.betTimeLimit = new $util.LongBits(object.betTimeLimit.low >>> 0, object.betTimeLimit.high >>> 0).toNumber(); + if (object.betSecondLimit != null) + message.betSecondLimit = object.betSecondLimit | 0; if (object.betOpts) { if (!Array.isArray(object.betOpts)) throw TypeError(".api.BetRole.betOpts: array expected"); @@ -6253,11 +6300,24 @@ export const api = $root.api = (() => { if (options.defaults) { object.betMin = 0; object.betMax = 0; + if ($util.Long) { + let long = new $util.Long(0, 0, false); + object.betTimeLimit = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long; + } else + object.betTimeLimit = options.longs === String ? "0" : 0; + object.betSecondLimit = 0; } if (message.betMin != null && message.hasOwnProperty("betMin")) object.betMin = message.betMin; if (message.betMax != null && message.hasOwnProperty("betMax")) object.betMax = message.betMax; + if (message.betTimeLimit != null && message.hasOwnProperty("betTimeLimit")) + if (typeof message.betTimeLimit === "number") + object.betTimeLimit = options.longs === String ? String(message.betTimeLimit) : message.betTimeLimit; + else + object.betTimeLimit = options.longs === String ? $util.Long.prototype.toString.call(message.betTimeLimit) : options.longs === Number ? new $util.LongBits(message.betTimeLimit.low >>> 0, message.betTimeLimit.high >>> 0).toNumber() : message.betTimeLimit; + if (message.betSecondLimit != null && message.hasOwnProperty("betSecondLimit")) + object.betSecondLimit = message.betSecondLimit; if (message.betOpts && message.betOpts.length) { object.betOpts = []; for (let j = 0; j < message.betOpts.length; ++j) diff --git a/src/api/websocket.js b/src/api/websocket.js index fe6aeca..450ccf9 100644 --- a/src/api/websocket.js +++ b/src/api/websocket.js @@ -12,7 +12,7 @@ const { api } = proto const websocket = { conn: null, - status: 0, // 0未初始,1打开,2已认证,3关闭,4连接中 TODO 未连接 + status: 0, // 0未初始,1打开,2已认证,3关闭,4连接中,5等待重连 token: '', seq: 1, // 消息序号,递增 offset: 0, @@ -33,7 +33,7 @@ const websocket = { conn.close(); } }, - init: async function({ offset, opFail, opSuccess, opPathMap, nameOpMap }, { wsAddr }) { // callback 连接失败不一定调用 + init: async function({ offset, opFail, opSuccess, opPathMap, nameOpMap }, { wsAddr }) { // this.status = 0; this.offset = offset || 0; this.opFail = opFail; @@ -70,7 +70,6 @@ const websocket = { store.dispatch(connected()); store.dispatch(setUserInfo(res.toJSON())); // console.log('identity success:', res) - try { callback(null) } catch(e) { console.error('error:', e); } // 依次发送等待连接的消息队列 for (let i = 0; i < this.waitInitCalls.length; i++) { @@ -92,34 +91,32 @@ const websocket = { showToast({title: err}); console.error('连接认证失败:', err); reject('连接认证失败:' + err); - - // try { callback(err) } catch(e) { console.error('ws identity callback error: ', e); } }) } ws.onerror = e => { this.closeConn(); - this.status = 3; - store.dispatch(disconnect()); - - clearInterval(this.reconnectInterval); + if (this.status !== 5) { + this.status = 3; + clearInterval(this.reconnectInterval); + } clearInterval(this.pingInterval); + store.dispatch(disconnect()); console.log('ws connect failed.'); reject('ws connect failed!'); } ws.onclose = e => { - this.status = 3; + clearInterval(this.pingInterval); store.dispatch(disconnect()); console.log('ws connection close.') - clearInterval(this.pingInterval); - - if (this.conn === ws) { - this.reconnectPolicy(); - // this.reconnectInterval = setInterval(() => { - // }, Math.random() * 3 + 4); + if (this.status !== 5) { + this.status = 3; + if (this.conn === ws) { + this.reconnectPolicy(); + } } } @@ -188,28 +185,54 @@ const websocket = { }, // 重新连接, reconnectPolicy() { - if (this.reconnectInterval < 0 && this.status === 4) { + if (this.status !== 3) { + if (isIn(this.status, 4, 5)) { + store.dispatch(connecting()); + } return; } - this.reconnectInterval = setInterval(async () => { + this.status = 5; + store.dispatch(connecting()); + const reconnect = async () => { console.log('ws reconnecting...'); const token = getStorage('_t'); if (!token) { + this.status = 3 clearInterval(this.reconnectInterval); this.reconnectInterval = -1; // 无token跳转到login setTimeout(() => redirectTo({url: '/pages/login/login'}), 500); return; } + + const res = await this.connect(); + console.log('reconnect success:', res); + // this.status = 3 + clearInterval(this.reconnectInterval); + this.reconnectInterval = -1; + + // try { + // }catch(err) { + // console.log('reconnect error:', err); + // } + } + setTimeout(async () => { try { - const res = await this.connect(); - console.log('reconnect success:', res); - clearInterval(this.reconnectInterval); - this.reconnectInterval = -1; + await reconnect(); }catch(err) { - console.log('reconnect error:', err); + console.log('first reconnect failed try 3s later.', err); + + // 后每3s重连下 + this.reconnectInterval = setInterval(async () => { + try { + await reconnect(); + }catch(err) { + console.log('reconnect failed.', err); + } + }, 10000); } - }, 3000); + // 首次重连随机时长 2-6s + }, (parseInt(Math.random() * 5) + 2) * 1000); }, waitCall: {}, // 等待响应的 callback 列表 runPingInterval() { @@ -221,7 +244,7 @@ const websocket = { store.dispatch(setTTL(ttl)); }, err => { - console.log('ping error:', err) + console.error('ping error:', err); } ); }, 10000); diff --git a/src/components/title.jsx b/src/components/title.jsx index 75c087d..2b04092 100644 --- a/src/components/title.jsx +++ b/src/components/title.jsx @@ -3,7 +3,8 @@ import cnames from 'classnames'; import { useSelector } from 'react-redux'; import { View, Text, Image } from '@tarojs/components'; import { Menu, MenuItem } from '@nutui/nutui-react-taro'; -import { navigateBack } from '@/utils/application'; +import { navigateBack, showToast } from '@/utils/application'; +import websocket from '@/api/websocket' import left from '@/assets/icon/left-1.svg'; import './style/title.scss'; @@ -14,6 +15,14 @@ function Title(props) { ...state.user, ...state.conn })); const { leftSolt, leftIcon, onLeftClick, rightSolt } = props; + + function reconnectNet(netStatus) { + if (netStatus === 1) { + console.log('reconnect net.') + websocket.reconnectPolicy(); + } + } + return ( @@ -22,14 +31,18 @@ function Title(props) { {leftSolt || } {/*1已断开,2连接中,3已连接*/} - + 500, })}> - {ttl + 'ms'} + { + netStatus === 3 ? ttl + 'ms' + : netStatus === 2 ? "连接中..." + : netStatus === 1 ? "离线(点击重连)" : "未知状态" + } diff --git a/src/pages/new_table/new_table.jsx b/src/pages/new_table/new_table.jsx index fd456d3..383d831 100644 --- a/src/pages/new_table/new_table.jsx +++ b/src/pages/new_table/new_table.jsx @@ -56,7 +56,7 @@ function NewTable() { return ( - + <Title title={""} bgColor={true} /> <View className="game-props"> <View className="prop-wrap"> <View className="prop-k"><Text>牌局类型:</Text></View> diff --git a/src/store/conn.js b/src/store/conn.js index a1b0e8d..63927bc 100644 --- a/src/store/conn.js +++ b/src/store/conn.js @@ -14,7 +14,7 @@ const connSlice = createSlice({ state.status = 3; }, disconnect: state => { - state.status = 4; + state.status = 1; }, setTTL: (state, action) => { state.ttl = action.payload;