Commit 6698c484 by 姜雷

修改洗浴socket重连逻辑

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