[unisocket.js] reconnecting 时发送 _passtoken 给服务器; [index.js] 优化 setBarTitles 使得调用时 call(this 可用也可不用。
This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user