Browse Source

ws reconnect

master
tangmingyou 4 years ago
parent
commit
67053df733
  1. 66
      src/api/websocket.js

66
src/api/websocket.js

@ -11,19 +11,24 @@ const { api } = proto
const websocket = { const websocket = {
conn: null, conn: null,
status: 0, // 0未初始,1打开,2已认证,3关闭 TODO 未连接,连接中 方法等待队列 status: 0, // 0未初始,1打开,2已认证,3关闭,4连接中 TODO 未连接
seq: 1, // 消息序号 token: '',
seq: 1, // 消息序号,递增
offset: 0, offset: 0,
opFail: 0, opFail: 0,
opSuccess: 0, opSuccess: 0,
opPathMap: {}, opPathMap: {},
nameOpMap: {}, nameOpMap: {},
wsAddr: '',
// 消息监听列表 // 消息监听列表
msgListener: {}, // {op: func} msgListener: {}, // {op: func}
waitInitCalls: [], waitInitCalls: [],
init(token, { offset, opFail, opSuccess, opPathMap, nameOpMap }, { wsAddr }) { reconnectInterval: -1,
init(token, { offset, opFail, opSuccess, opPathMap, nameOpMap }, { wsAddr }, callback) { // callback 连接失败不一定调用
// TODO 连接状态判定 this.status = 4;
callback = callback || (() => {});
// 连接状态判定
this.token = token;
this.offset = offset || 0; this.offset = offset || 0;
this.opFail = opFail; this.opFail = opFail;
this.opSuccess = opSuccess; this.opSuccess = opSuccess;
@ -39,9 +44,12 @@ const websocket = {
console.log(this) console.log(this)
ws.binaryType = 'arraybuffer' ws.binaryType = 'arraybuffer'
ws.onopen = (e) => { ws.onopen = (e) => {
// 关闭定时重连
clearInterval(this.reconnectInterval);
this.status = 1; this.status = 1;
// 连接后立即认证连接 // 连接后立即认证连接
const req = api.ReqIdentity.create({ token }) const req = api.ReqIdentity.create({ token });
// const reqBuffer = api.ReqIdentity.encode(req).finish() // const reqBuffer = api.ReqIdentity.encode(req).finish()
this.send(req, res => { this.send(req, res => {
this.status = 2; this.status = 2;
@ -49,8 +57,8 @@ const websocket = {
// window.res = res; // window.res = res;
// console.log(store, setUserInfo(res)) // console.log(store, setUserInfo(res))
store.dispatch(setUserInfo(res.toJSON())); store.dispatch(setUserInfo(res.toJSON()));
console.log('identity success:', res) console.log('identity success:', res)
try { callback(null) } catch(e) { console.error('error:', e); }
// 依次发送等待连接的消息队列 // 依次发送等待连接的消息队列
for (let i = 0; i < this.waitInitCalls.length; i++) { for (let i = 0; i < this.waitInitCalls.length; i++) {
@ -63,8 +71,29 @@ const websocket = {
removeStorage('_t') removeStorage('_t')
showToast({title: err}) showToast({title: err})
console.error('连接认证失败:', err) console.error('连接认证失败:', err)
try { callback(err) } catch(e) { console.error('error:', e); }
}) })
} }
ws.onerror = (a, b, c) => {
this.status = 3;
console.log('ws error:', a, b, c)
this.reconnectInterval = setInterval(() => {
this.reconnect();
}, Math.random() * 3 + 3);
}
ws.onclose = e => {
this.status = 3;
console.log('ws close', e)
this.reconnectInterval = setInterval(() => {
this.reconnect();
}, Math.random() * 3 + 3);
}
ws.onmessage = (e) => { ws.onmessage = (e) => {
// console.log('msg:', e.data) // console.log('msg:', e.data)
const wrap = api.ProtoWrap.decode(new Uint8Array(e.data)) const wrap = api.ProtoWrap.decode(new Uint8Array(e.data))
@ -112,22 +141,17 @@ const websocket = {
console.log('no handler msg:', wrap.op, res) console.log('no handler msg:', wrap.op, res)
} }
ws.onerror = (a, b, c) => {
this.status = 3;
console.log('ws error:', a, b, c)
// var interval = setInterval(() => {
// this.reconnect();
// }, Math.random() * 3);
}
ws.onclose = e => {
this.status = 3;
console.log('ws close', e)
}
}, },
// 重新连接,
reconnect() { reconnect() {
if (this.status === 4) {
return;
}
console.log('ws reconnecting...');
// TODO store 状态
this.init(this.token, this, this, err => {
console.log('reconnect end:', err)
});
}, },
waitCall: {}, // 等待响应的 callback 列表 waitCall: {}, // 等待响应的 callback 列表
send(msg, resCall, errCall) { send(msg, resCall, errCall) {

Loading…
Cancel
Save