|
|
@ -11,7 +11,7 @@ |
|
|
|
<script> |
|
|
|
export default { |
|
|
|
data() { |
|
|
|
return { tcp: null }; |
|
|
|
return { tcp: null, isConnected: false, isSend: false }; |
|
|
|
}, |
|
|
|
|
|
|
|
props: { |
|
|
@ -23,35 +23,70 @@ export default { |
|
|
|
onLoad() {}, |
|
|
|
methods: { |
|
|
|
sendTCP() { |
|
|
|
// this.$t.ui.showLoading('发送中...'); |
|
|
|
let that = this; |
|
|
|
console.log('发送'); |
|
|
|
if (!this.tcp) { |
|
|
|
this.tcp = wx.createTCPSocket(); |
|
|
|
if (!that.tcp) { |
|
|
|
that.tcp = wx.createTCPSocket(); |
|
|
|
} |
|
|
|
console.log('this.tcp', this.tcp); |
|
|
|
this.tcp.connect({ |
|
|
|
address: '192.168.4.1', |
|
|
|
port: 1001, |
|
|
|
console.log('that.tcp', that.tcp); |
|
|
|
if (!that.isConnected) { |
|
|
|
that.tcp.connect({ |
|
|
|
address: '192.168.4.1', |
|
|
|
port: 1001, |
|
|
|
}); |
|
|
|
} |
|
|
|
console.log('连接this.tcp', that.tcp); |
|
|
|
if (that.isConnected) { |
|
|
|
that.tcp.write(`#${that.name}:${that.password}$`); |
|
|
|
that.tcp.write(`#${that.name}:${that.password}$`); |
|
|
|
console.log('write-this.tcp', that.tcp); |
|
|
|
} |
|
|
|
|
|
|
|
// 监听连接状态 |
|
|
|
that.tcp.onConnect(() => { |
|
|
|
console.log('监听连接状态'); |
|
|
|
that.isConnected = true; |
|
|
|
if (!that.isSend) { |
|
|
|
console.log('监听连接状态,发送'); |
|
|
|
that.tcp.write(`#${that.name}:${that.password}$`); |
|
|
|
that.tcp.write(`#${that.name}:${that.password}$`); |
|
|
|
} |
|
|
|
}); |
|
|
|
console.log('链接this.tcp', this.tcp); |
|
|
|
this.tcp.write(`#${this.name}:${this.password}$`); |
|
|
|
this.tcp.write(`#${this.name}:${this.password}$`); |
|
|
|
console.log('write-this.tcp', this.tcp); |
|
|
|
|
|
|
|
this.tcp.onMessage((message, remoteInfo, localInfo) => { |
|
|
|
// this.$t.ui.hideLoading(); |
|
|
|
// 监听消息 返回上一页 |
|
|
|
that.tcp.onMessage((message, remoteInfo, localInfo) => { |
|
|
|
console.log(message, remoteInfo, localInfo); |
|
|
|
// 如果成功,关掉 tcp 返回上一页 |
|
|
|
// this.tcp = null; |
|
|
|
// that.tcp = null; |
|
|
|
if (message) { |
|
|
|
this.tcp = null; |
|
|
|
this.tcp.close(); |
|
|
|
this.$t.page.openPage('/pagesYanyuan/hold-all/hold-all'); |
|
|
|
console.log('成功, 返回上一页', that.tcp); |
|
|
|
that.isSend = true; |
|
|
|
that.tcp.close(); |
|
|
|
console.log('关闭 跳转'); |
|
|
|
that.$t.page.openPage('/pagesYanyuan/hold-all/hold-all'); |
|
|
|
} |
|
|
|
}); |
|
|
|
// setTimeout(() => { |
|
|
|
// this.$t.ui.hideLoading(); |
|
|
|
// }, 10000); |
|
|
|
|
|
|
|
// 断开连接 |
|
|
|
that.tcp.offConnect(() => { |
|
|
|
console.log('断开连接', that.isSend); |
|
|
|
if (!that.isSend) { |
|
|
|
that.tcp.connect({ |
|
|
|
address: '192.168.4.1', |
|
|
|
port: 1001, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
that.isConnected = false; |
|
|
|
that.isSend = false; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
// 关闭 |
|
|
|
that.tcp.onClose(() => { |
|
|
|
that.isConnected = false; |
|
|
|
that.isSend = false; |
|
|
|
that.tcp = null; |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
}; |
|
|
|