Commit 6698c484 by 姜雷

修改洗浴socket重连逻辑

parent 9648e5ba
......@@ -45,8 +45,6 @@ type IProps = PageStateProps & PageDispatchProps;
type PageState = {
bluetoothState: boolean;
task: Taro.SocketTask | null;
timer: number | undefined;
reConnectting: boolean;
deviceInfo: DeviceInfo;
sockedDone: boolean;
deviceDone: boolean;
......@@ -59,6 +57,9 @@ interface Shower {
state: PageState;
}
let timer: number | null = null;
let reConnectting: boolean = false;
@connect(
({ userinfo, Shower }) => ({
userinfo,
......@@ -80,8 +81,6 @@ class Shower extends Component {
this.state = {
bluetoothState: false,
task: null,
timer: undefined,
reConnectting: false,
sockedDone: false,
deviceDone: false,
showerState: false,
......@@ -114,7 +113,6 @@ class Shower extends Component {
connectSocket(SOCKET_URL).then(task => {
task.onOpen(() => {
console.log('onOpen');
const { reConnectting } = this.state;
if (reConnectting) {
this.reConnectDeviceSocket(true);
}
......@@ -149,28 +147,28 @@ class Shower extends Component {
}
});
task.onClose(e => {
console.log('socked关闭', e);
console.log('socked关闭', e, reConnectting, timer);
this.setState({
task: null,
sockedDone: false,
});
if (e.code === StopCode) {
console.log('正确结束socket连接');
} else {
} else if (!reConnectting) {
console.log('开始重连socket');
this.reConnectDeviceSocket(false);
} else {
console.log('断开socket', e);
}
});
});
}
reConnectDeviceSocket(cancel: boolean) {
const { timer, reConnectting } = this.state;
if (cancel) {
clearTimeout(timer);
this.setState({
timer: null,
reConnectting: false,
});
timer && clearTimeout(timer);
timer = null;
reConnectting = false;
return;
}
console.log(reConnectting, timer);
......@@ -179,10 +177,8 @@ class Shower extends Component {
this.connectDeviceSocket();
} else if (timer) {
clearTimeout(timer);
this.setState({
timer: null,
reConnectting: false,
});
reConnectting = false;
timer = null;
console.log('请保证网络正常');
Taro.showModal({
title: '警告',
......@@ -190,15 +186,11 @@ class Shower extends Component {
});
} else {
let newTimer = setTimeout(() => {
this.setState({
reConnectting: false,
});
reConnectting = false;
}, 10000);
reConnectting = true;
timer = newTimer;
console.log(reConnectting, timer);
this.setState({
timer: newTimer,
reConnectting: true,
});
this.connectDeviceSocket();
}
}
......@@ -224,8 +216,8 @@ class Shower extends Component {
}
closeDeviceSocket() {
const { task, timer } = this.state;
console.log('in close', task);
const { task } = this.state;
console.log('in close', task, timer);
if (task) {
task.close({
code: StopCode,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment