|
|
import config from '@/config'
|
|
|
import { getuserId } from '@/utils/auth'
|
|
|
import { createAlarm } from '@/components/nativeMsg.js';
|
|
|
// import { getXT } from '@/minix/MyRingTone.js'
|
|
|
//导入链接
|
|
|
const socketData = {
|
|
|
state: {
|
|
|
xintiao: 0, //心跳,用来测试的时候判断websocket是否正常
|
|
|
// userId: getuserId(), //用户id
|
|
|
socketTast: null, //socket实例
|
|
|
websocketData: {}, // 存放从后端接收到的websocket数据
|
|
|
|
|
|
webSocketPingTimer: null, // 心跳定时器
|
|
|
webSocketPingTime: 10000, // 心跳的间隔,当前为 10秒,
|
|
|
webSocketReconnectCount: 0, // 重连次数
|
|
|
webSocketIsReconnect: true, // 是否重连
|
|
|
webSocketIsOpen: true, //链接是否在打开
|
|
|
|
|
|
},
|
|
|
mutations: {
|
|
|
setXinTiao(state, data) {
|
|
|
state.xintiao = state.xintiao + data
|
|
|
},
|
|
|
setsockTast(state, data) {
|
|
|
state.socketTast = data
|
|
|
},
|
|
|
setWebsocketData(state, data) {
|
|
|
state.websocketData = data
|
|
|
},
|
|
|
setChonglian(state, data) {
|
|
|
state.webSocketReconnectCount = state.webSocketReconnectCount + data
|
|
|
},
|
|
|
setIsOpen(state, data) {
|
|
|
state.webSocketIsOpen = data
|
|
|
},
|
|
|
|
|
|
},
|
|
|
actions: {
|
|
|
//链接打开
|
|
|
websocketOnOpen({ dispatch, commit }) {
|
|
|
console.log('WebSocket连接正常打开中...!')
|
|
|
//开始心跳检测
|
|
|
dispatch('webSocketPing')
|
|
|
commit('setIsOpen', true)
|
|
|
},
|
|
|
// 定时心跳告诉服务器自己还活着,防止丢包
|
|
|
webSocketPing({ state, dispatch, commit }) {
|
|
|
if (getuserId()) {
|
|
|
state.webSocketPingTimer = setTimeout(() => {
|
|
|
//链接关闭就结束心跳
|
|
|
if (!state.webSocketIsOpen) {
|
|
|
return false;
|
|
|
}
|
|
|
console.log("心跳");
|
|
|
commit('setXinTiao', 1)
|
|
|
const payload = 1
|
|
|
|
|
|
dispatch('websocketSend', JSON.stringify(payload));
|
|
|
clearTimeout(state.webSocketPingTimer);
|
|
|
// 重新执行
|
|
|
dispatch('webSocketPing');
|
|
|
}, state.webSocketPingTime);
|
|
|
}
|
|
|
|
|
|
},
|
|
|
websocketInit({ state, dispatch, commit }, ) {
|
|
|
if (getuserId()) {
|
|
|
console.log(`${config.WebSocketBaseUrl}/${getuserId()}`);
|
|
|
let socketTast = uni.connectSocket({
|
|
|
// url, // url是websocket连接ip
|
|
|
url: `${config.WebSocketBaseUrl}/${getuserId()}`,
|
|
|
success: () => {
|
|
|
// uni.showToast({
|
|
|
// title: 'websocketInit',
|
|
|
// icon: 'none'
|
|
|
// });
|
|
|
// console.log('websocket连接成功!')
|
|
|
},
|
|
|
fail: e => {
|
|
|
console.log(e)
|
|
|
}
|
|
|
})
|
|
|
commit('setsockTast', socketTast)
|
|
|
|
|
|
//检测链接打开
|
|
|
state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
|
|
|
//接收服务器消息
|
|
|
state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
|
|
|
// 链接关闭事件
|
|
|
state.socketTast.onClose(e => dispatch('websocketOnClose'))
|
|
|
//链接错误
|
|
|
state.socketTast.onError(e => dispatch('websocketOnError'))
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
// 断开连接时
|
|
|
webSocketClose({ state, dispatch, commit }) {
|
|
|
if (getuserId()) {
|
|
|
// 修改状态为未连接
|
|
|
commit('setIsOpen', false)
|
|
|
// state.webSocketIsOpen = false;
|
|
|
// state.webSocket = null;
|
|
|
// // 判断是否重连
|
|
|
if (
|
|
|
state.webSocketIsReconnect &&
|
|
|
state.webSocketReconnectCount === 0
|
|
|
) {
|
|
|
// console.log("正在尝试重连");
|
|
|
// 第一次直接尝试重连
|
|
|
dispatch('webSocketReconnect');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
},
|
|
|
// WebSocket 重连
|
|
|
webSocketReconnect({ state, dispatch, commit }) {
|
|
|
if (getuserId()) {
|
|
|
if (state.webSocketIsOpen) {
|
|
|
return false;
|
|
|
}
|
|
|
console.log("第" + state.webSocketReconnectCount + "次重连")
|
|
|
commit('setChonglian', 1)
|
|
|
// 判断是否到了最大重连次数
|
|
|
|
|
|
// 初始化
|
|
|
console.log("开始重连")
|
|
|
dispatch('websocketInit');
|
|
|
uni.showToast({
|
|
|
title: `第${state.webSocketReconnectCount}次重连`,
|
|
|
icon: 'none'
|
|
|
});
|
|
|
// 每过 5 秒尝试一次,检查是否连接成功,直到超过最大重连次数
|
|
|
let timer = setTimeout(() => {
|
|
|
dispatch('webSocketReconnect');
|
|
|
clearTimeout(timer);
|
|
|
}, 5000);
|
|
|
}
|
|
|
},
|
|
|
// 收到数据
|
|
|
websocketOnMessage({ commit }, res) {
|
|
|
// 修改状态为未连接
|
|
|
//接到推送的消息--显示全局弹窗
|
|
|
if (res.data !== 'sssss' && res.data !== '连接成功') {
|
|
|
// #ifdef APP-VUE
|
|
|
uni.createPushMessage({
|
|
|
title: "任务提醒",
|
|
|
sound: "system",
|
|
|
content: JSON.parse(res.data).messageContent,
|
|
|
success(res) {
|
|
|
console.log('推送成功', res)
|
|
|
},
|
|
|
fail(err) {
|
|
|
console.log('推送失败', err)
|
|
|
}
|
|
|
})
|
|
|
// getXT('您有一条推送消息')
|
|
|
//内部悬浮弹窗
|
|
|
let alarmId = Date.now();
|
|
|
let msg = {
|
|
|
alarmId: alarmId,
|
|
|
warningTypeStr: '任务提示',
|
|
|
projectName: '2023年5月17日',
|
|
|
description: JSON.parse(res.data).messageContent
|
|
|
};
|
|
|
|
|
|
let alarmIns = createAlarm(msg, res => {
|
|
|
const { type, result } = res;
|
|
|
//监听点击事件
|
|
|
if (type === 'click') {
|
|
|
uni.showToast({
|
|
|
title: '点击了',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
uni.showToast({
|
|
|
title: `${type === 'click' ? '点击了' : '上滑了'}`,
|
|
|
icon: 'none'
|
|
|
});
|
|
|
});
|
|
|
|
|
|
// #endif
|
|
|
// #ifdef H5
|
|
|
//弹窗
|
|
|
this._vm.$openInvite(JSON.parse(res.data).messageContent)
|
|
|
// #endif
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('收到服务器内容:' + res.data.toString().slice(1, 11))
|
|
|
if (res.data !== '连接成功') {
|
|
|
commit('setWebsocketData', (res.data || null))
|
|
|
}
|
|
|
},
|
|
|
//链接关闭
|
|
|
websocketOnClose({ commit, dispatch }) {
|
|
|
//链接关闭执行
|
|
|
dispatch('webSocketClose')
|
|
|
uni.showToast({
|
|
|
title: 'websocketOnError连接关闭',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
console.log('websocketOnClose连接关闭')
|
|
|
},
|
|
|
//连接错误
|
|
|
websocketOnError({ commit, dispatch }) {
|
|
|
|
|
|
//链接关闭执行
|
|
|
dispatch('webSocketClose')
|
|
|
uni.showToast({
|
|
|
title: 'websocketOnError连接错误',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
console.log('websocketOnError连接错误')
|
|
|
},
|
|
|
//关闭websocket
|
|
|
websocketCloseGuanBi({ state }) {
|
|
|
if (!state.socketTast) return
|
|
|
state.socketTast.close({
|
|
|
success(res) {
|
|
|
console.log('关闭成功', res)
|
|
|
},
|
|
|
fail(err) {
|
|
|
console.log('关闭失败', err)
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
// 发送数据
|
|
|
websocketSend({ state, dispatch }, data) {
|
|
|
state.socketTast.send({
|
|
|
data,
|
|
|
success: res => {
|
|
|
console.log('发送成功', res)
|
|
|
},
|
|
|
fail: e => {
|
|
|
console.log('发送失败', e)
|
|
|
dispatch('webSocketClose')
|
|
|
uni.showToast({
|
|
|
title: '发送失败',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
export default socketData |