添加socket

This commit is contained in:
A1300399510 2023-09-20 15:51:18 +08:00
parent 787f4e8094
commit e1bcfc00c5
4 changed files with 55 additions and 11 deletions

0
package-lock.json generated Executable file → Normal file
View File

View File

@ -15,24 +15,75 @@ export default {
pathname: "",
parameterObject: {},
isPc: false,
socketTask: null,
}
},
watch: {
},
mounted() {
this.$store.dispatch('fetchHistoricalSearch') //
this.$store.dispatch('getAllForum', this) //
this.isJudgePc()
// this.$skipUrl("gggg", false)
// setTimeout(() => {
// // this.hasMiucmsSessionCookie()
// // this.useSocket
// console.log(this.$store.state['user']);
// }, 1000)
let timer = setInterval(() => {
if (Object.keys(this.$store.state.user).length !== 0) {
this.useSocket()
clearInterval(timer)
}
}, 1000);
},
methods: {
isJudgePc() {
// socket
useSocket() {
let token = this.getMiucmsSessionCookie() || ''
this.socketTask = new WebSocket(`wss://app.gter.net/socket?token=${token}`);
this.socketTask.onopen = () => {
let user = this.$store.state.user || {};
if (user && token) {
this.socketTask.send(JSON.stringify({
type: 'bind',
data: {
token,
uid: user.uid || 0
}
}))
}
setTimeout(() => this.timedTransmission(), 50000);
};
this.socketTask.onclose = () => {
console.log('socket关闭了', new Date());
};
},
//
timedTransmission() {
if (this.socketTask.readyState != 1) return
this.socketTask.send(JSON.stringify({ type: 'ping' }))
setTimeout(() => this.timedTransmission(), 50000)
},
getMiucmsSessionCookie() {
let cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i].split("=");
if (cookie[0].trim() === "miucms_session") {
return cookie[1];
}
}
return null;
},
isJudgePc() {
const userAgentInfo = navigator.userAgent;
const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
let flag = true;
@ -43,8 +94,6 @@ export default {
}
}
if (flag) {
let pathname = window.location.pathname

View File

@ -72,17 +72,12 @@ export default new Vuex.Store({
// 获取全部板块的数据
getAllForum({ commit }, that) {
// getAllForum(commit, that) {
// that.$startupUnderLoading(that)
that.$http.get("/api/home/allForum").then(res => {
if (res.code != 200) return;
let allForumList = res.data
commit('setAllForumList', allForumList)
}).catch(err => {
that.$message.error(err.message)
}).finally(() => {
// that.$closeUnderLoading(that)
})
},

View File

@ -31,7 +31,7 @@ service.interceptors.request.use(config => {
// config['headers']['authorization'] = "qj2q1qk1on0curclipghjtv5ja1g9eq2"
// config['headers']['authorization'] = "661aiz52k5e6vqgmkxnz0wvbv8nciz8h"
if (process.env.NODE_ENV == "development") config['headers']['authorization'] = "uqw2cz1zdqf2uvcd22kxxy5gtsrrctiz"
if (process.env.NODE_ENV == "development") config['headers']['authorization'] = "0h870ovk2xckoqfsh8a3t3sg4sg5z7eg"
return config
}, error => {