From a750520d19f67470443253751db8c86d204e5481 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sat, 6 Jun 2020 17:05:30 +0800 Subject: [PATCH] =?UTF-8?q?[unisocket.js]=20reconnecting=20=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=20=5Fpasstoken=20=E7=BB=99=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8;=20[index.js]=20=E4=BC=98=E5=8C=96=20setBarTitles=20?= =?UTF-8?q?=E4=BD=BF=E5=BE=97=E8=B0=83=E7=94=A8=E6=97=B6=20call(this=20?= =?UTF-8?q?=E5=8F=AF=E7=94=A8=E4=B9=9F=E5=8F=AF=E4=B8=8D=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 7 ++++--- unisocket/unisocket.js | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index f5ace07..cb2e25e 100644 --- a/index.js +++ b/index.js @@ -96,11 +96,12 @@ module.exports = { }, setBarTitles({windowTitle, pageTitle}={}){ - uni.setNavigationBarTitle({ title: pageTitle || this.localeText.tPageTitle }) + let page = this.$store ? this : getCurrentPages().pop() + uni.setNavigationBarTitle({ title: pageTitle || page.localeText.tPageTitle || page.i18nText[page.$store.i18n.activeLang].tPageTitle }) // #ifdef H5 - document.title = windowTitle || this.appName || this.$store.getters['i18n/getAppName'] // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。 + document.title = windowTitle || page.appName || page.$store.getters['i18n/getAppName'] // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。 // #endif - this.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才能成功 + page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才能成功 }, formatMoney(value, decimal){ diff --git a/unisocket/unisocket.js b/unisocket/unisocket.js index c66190f..e6b4eb9 100644 --- a/unisocket/unisocket.js +++ b/unisocket/unisocket.js @@ -5,7 +5,7 @@ const my = { } module.exports={ - initSocket(url){ + initSocket(url, reconnect=false){ if (!my.socket || my.socket.readyState!==my.socket.OPEN && typeof(url)==='string') { console.log('WebSocket connecting...') my.socket = uni.connectSocket({ @@ -16,13 +16,19 @@ module.exports={ console.log('WebSocket onOpen: ', res) clearInterval(my.reconnecting) delete my.reconnecting + + if (reconnect && uni.getStorageSync('_passtoken')) { + console.log('Reporting owner for reconnecting socket') + my.socket.send({data: JSON.stringify({skevent:'SOCKET_OWNER', _passtoken: uni.getStorageSync('_passtoken')})}) + } + }) my.socket.onClose((res)=>{ console.log('Websocket onClose: ', res) if (!my.reconnecting) my.reconnecting = setInterval(()=>{ console.log(new Date().toJSON(), 'WebSocket reconnecting...') - this.initSocket(url) + this.initSocket(url, true) }, 5000) // 每5秒尝试重连 }) my.socket.onError((err)=>{ @@ -60,12 +66,11 @@ module.exports={ } return this }, - sendObject(data){ - if (typeof(data)!=='string'){ - data=JSON.stringify(data) - } + sendObject(dataObj){ if (my.socket && my.socket.readyState===my.socket.OPEN){ - my.socket.send({data}) + my.socket.send({ + data: typeof(dataObj)!=='string' ? JSON.stringify(dataObj) : dataObj + }) } } -} \ No newline at end of file +}