122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
var websocket = function(addr) {
|
|
var app = getApp();
|
|
var thos = this;
|
|
this.buffer = new Array(); //发送数据缓冲区
|
|
this.reOpenData = [2, 5, 3, 5, 5, 3, 5, 8, 10, 20, 30, 30]; //断线重连,每次间隔时间/秒
|
|
this.reOpen = 0; //断线重连次数
|
|
this.keepAliveTimer = setInterval(function() {
|
|
console.log('keep');
|
|
try {
|
|
if (thos.wsock && thos.wsock.readyState !== 1) {
|
|
thos.reOpenSec--;
|
|
if (thos.reOpenSec <= 0) {
|
|
thos.reOpen++;
|
|
thos.reOpenSec = 10;
|
|
thos.open();
|
|
}
|
|
return;
|
|
}
|
|
if (thos.buffer.length > 0) {
|
|
var data = thos.buffer.pop();
|
|
thos.send(data);
|
|
} else {
|
|
if (new Date().getTime() - thos.last_health_time >= 15000 && thos.wsock
|
|
.readyState === 1) {
|
|
thos.wsock.send({
|
|
data: 'h'
|
|
});
|
|
thos.last_health_time = new Date().getTime();
|
|
}
|
|
}
|
|
} catch (err) {
|
|
app.uperr("app.ws.error", err);
|
|
}
|
|
}, 1000);
|
|
this.setOpenSec = function() {
|
|
(thos.reOpen < thos.reOpenData.length) ? thos.reOpenSec = thos.reOpenData[thos.reOpen]: thos
|
|
.reOpenSec = 90; //断线重试太多后的间隔时间
|
|
}
|
|
this.open = function() {
|
|
try {
|
|
if (thos.wsock && thos.wsock.trueClose)
|
|
return;
|
|
thos.setOpenSec();
|
|
if (thos.wsock) {
|
|
if (thos.wsock.readyState == 1)
|
|
return;
|
|
thos.wsock.close();
|
|
}
|
|
thos.wsock = uni.connectSocket({
|
|
url: addr,
|
|
complete: () => {}
|
|
});
|
|
thos.wsock.onOpen(function(event) {
|
|
try {
|
|
thos.reOpen = 0;
|
|
thos.last_health_time = new Date().getTime();
|
|
if (typeof(thos.onopen) == "function")
|
|
thos.onopen(event);
|
|
} catch (err) {
|
|
app.uperr('app.ws.onopen', err);
|
|
}
|
|
});
|
|
thos.wsock.onMessage(function(e) {
|
|
try {
|
|
if (typeof(thos.onmessage) == "function")
|
|
thos.onmessage(e);
|
|
} catch (err) {
|
|
app.uperr('app.ws.onmessage', err);
|
|
}
|
|
});
|
|
thos.wsock.onClose(function(e) {
|
|
try {
|
|
if (thos.wsock.trueClose) {
|
|
clearInterval(thos.keepAliveTimer);
|
|
return;
|
|
}
|
|
thos.setOpenSec();
|
|
if (typeof(thos.onclose) == "function")
|
|
thos.onclose(e);
|
|
} catch (err) {
|
|
app.uperr('app.ws.onclose', err);
|
|
}
|
|
});
|
|
thos.wsock.onError(function(e) {
|
|
try {
|
|
thos.setOpenSec();
|
|
if (typeof(thos.onerror) == "function")
|
|
thos.onerror(e);
|
|
} catch (err) {
|
|
app.uperr('app.ws.onerror', err);
|
|
}
|
|
});
|
|
} catch (err) {
|
|
console.log(app, err);
|
|
app.uperr('app.ws.open', err);
|
|
}
|
|
}
|
|
this.send = function(msg) {
|
|
try {
|
|
if (this.wsock === undefined || this.wsock.trueClose)
|
|
return false;
|
|
if (this.wsock.readyState !== 1 || this.wsock.bufferedAmount > 0)
|
|
this.buffer.push(msg);
|
|
else {
|
|
this.wsock.send({
|
|
data: msg
|
|
});
|
|
this.last_health_time = new Date().getTime();
|
|
}
|
|
return true;
|
|
} catch (err) {
|
|
app.uperr('app.ws.send', err);
|
|
}
|
|
}
|
|
this.close = function(send) {
|
|
this.wsock.trueClose = true;
|
|
this.wsock.close();
|
|
}
|
|
this.open();
|
|
};
|
|
|
|
export default websocket |