添加socket
This commit is contained in:
parent
787f4e8094
commit
e1bcfc00c5
0
package-lock.json
generated
Executable file → Normal file
0
package-lock.json
generated
Executable file → Normal file
59
src/App.vue
59
src/App.vue
@ -15,24 +15,75 @@ export default {
|
|||||||
pathname: "",
|
pathname: "",
|
||||||
parameterObject: {},
|
parameterObject: {},
|
||||||
isPc: false,
|
isPc: false,
|
||||||
|
socketTask: null,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
|
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
|
|
||||||
this.$store.dispatch('fetchHistoricalSearch') // 获取历史搜索记录
|
this.$store.dispatch('fetchHistoricalSearch') // 获取历史搜索记录
|
||||||
this.$store.dispatch('getAllForum', this) // 获取全部板块
|
this.$store.dispatch('getAllForum', this) // 获取全部板块
|
||||||
|
|
||||||
this.isJudgePc()
|
this.isJudgePc()
|
||||||
|
|
||||||
// this.$skipUrl("gggg", false)
|
// 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: {
|
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 userAgentInfo = navigator.userAgent;
|
||||||
const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
|
const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
|
||||||
let flag = true;
|
let flag = true;
|
||||||
@ -43,8 +94,6 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (flag) {
|
if (flag) {
|
||||||
let pathname = window.location.pathname
|
let pathname = window.location.pathname
|
||||||
|
|
||||||
|
@ -72,17 +72,12 @@ export default new Vuex.Store({
|
|||||||
|
|
||||||
// 获取全部板块的数据
|
// 获取全部板块的数据
|
||||||
getAllForum({ commit }, that) {
|
getAllForum({ commit }, that) {
|
||||||
// getAllForum(commit, that) {
|
|
||||||
// that.$startupUnderLoading(that)
|
|
||||||
that.$http.get("/api/home/allForum").then(res => {
|
that.$http.get("/api/home/allForum").then(res => {
|
||||||
if (res.code != 200) return;
|
if (res.code != 200) return;
|
||||||
let allForumList = res.data
|
let allForumList = res.data
|
||||||
commit('setAllForumList', allForumList)
|
commit('setAllForumList', allForumList)
|
||||||
|
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
that.$message.error(err.message)
|
that.$message.error(err.message)
|
||||||
}).finally(() => {
|
|
||||||
// that.$closeUnderLoading(that)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
|
@ -31,7 +31,7 @@ service.interceptors.request.use(config => {
|
|||||||
// config['headers']['authorization'] = "qj2q1qk1on0curclipghjtv5ja1g9eq2"
|
// config['headers']['authorization'] = "qj2q1qk1on0curclipghjtv5ja1g9eq2"
|
||||||
// config['headers']['authorization'] = "661aiz52k5e6vqgmkxnz0wvbv8nciz8h"
|
// 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
|
return config
|
||||||
}, error => {
|
}, error => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user