|
@@ -23,13 +23,26 @@ class Index extends Component {
|
|
|
uObj: {},
|
|
|
chatUserObj: {},
|
|
|
viewId: '',
|
|
|
+ socketTimer: null
|
|
|
}
|
|
|
}
|
|
|
|
|
|
config = {
|
|
|
- navigationBarTitleText: '在线聊天',
|
|
|
+ navigationBarTitleText: '在线沟通',
|
|
|
}
|
|
|
|
|
|
+ componentWillUnmount () {
|
|
|
+ let pages = getCurrentPages()
|
|
|
+ let prevPage = pages[ pages.length - 2 ]
|
|
|
+ prevPage.$component.getNotifycount()
|
|
|
+ this.closeSocket()
|
|
|
+ }
|
|
|
+
|
|
|
+ closeSocket () {
|
|
|
+ Taro.closeSocket()
|
|
|
+ const { socketTimer } = this.state
|
|
|
+ clearInterval(socketTimer)
|
|
|
+ }
|
|
|
|
|
|
componentDidMount () {
|
|
|
const that = this
|
|
@@ -130,9 +143,12 @@ class Index extends Component {
|
|
|
// const { to_user_id } = that.$router.params
|
|
|
let token = Taro.getStorageSync('APP_token')
|
|
|
task.send({ data: '{"seq":"' + that.sendId() + '","cmd":"login","data":{"userId":"' + uObj.user_id + '","appId":101,"serviceToken":"' + token + '"}}' })
|
|
|
- setInterval(() => {
|
|
|
+ const socketTimer = setInterval(() => {
|
|
|
task.send({data: '{"seq":"' + that.sendId() + '","cmd":"heartbeat","data":{}}'});
|
|
|
}, 30000)
|
|
|
+ that.setState({
|
|
|
+ socketTimer
|
|
|
+ })
|
|
|
})
|
|
|
task.onMessage(function (msg) {
|
|
|
// console.log('onMessage: ', msg)
|
|
@@ -158,6 +174,23 @@ class Index extends Component {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+ if (data.cmd === 'heartbeat') {
|
|
|
+ const res = data.response || {}
|
|
|
+ if (res.code !== 200) {
|
|
|
+ msgSocketList.push({msg: res.codeMsg + '|暂无法接收消息,请退出当前页面重新登录~'})
|
|
|
+ }
|
|
|
+ that.setState({
|
|
|
+ msgSocketList
|
|
|
+ }, () => {
|
|
|
+ setTimeout(() => {
|
|
|
+ that.setState({
|
|
|
+ viewId: `item${msgSocketList.length - 1}`
|
|
|
+ }, () => {
|
|
|
+ that.closeSocket()
|
|
|
+ })
|
|
|
+ }, 100)
|
|
|
+ })
|
|
|
+ }
|
|
|
// task.close()
|
|
|
})
|
|
|
task.onError(function () {
|