change some files.
This commit is contained in:
parent
95fbe0d526
commit
d619c5800c
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"BACKEND_API": "123.207.17.178:8000"
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
// request.js
|
||||
import { BACKEND_API } from '@/config.json'
|
||||
import axios from 'axios'
|
||||
import Vue from 'vue'
|
||||
|
||||
// 创建 axios 实例
|
||||
const service = axios.create({
|
||||
baseURL: "http://123.207.17.178:8000", // API 的 base_url
|
||||
baseURL: `http://${BACKEND_API}`, // API 的 base_url
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// },
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
// websocket.js
|
||||
import { BACKEND_API } from '@/config.json'
|
||||
|
||||
const socketURL = 'ws://123.207.17.178:8000/api/ws'
|
||||
const socketURL = `ws://${BACKEND_API}/api/ws`
|
||||
|
||||
const websocketPlugin = {}
|
||||
|
||||
websocketPlugin.install = function (Vue) {
|
||||
const socket = new WebSocket(socketURL)
|
||||
|
||||
const sendHeartbeat = () => {
|
||||
const heartbeatMessage = {
|
||||
type: 'ping',
|
||||
}
|
||||
const message = JSON.stringify(heartbeatMessage)
|
||||
socket.send(message)
|
||||
}
|
||||
|
||||
socket.onopen = () => {
|
||||
console.log('WebSocket 连接已打开')
|
||||
// 在连接成功后发送特定消息
|
||||
|
||||
// 发送身份认证信息
|
||||
if (localStorage.getItem('userid')) {
|
||||
const type = 'auth'
|
||||
const role = 'student' // 或者从其他地方获取角色信息
|
||||
|
@ -22,11 +31,23 @@ websocketPlugin.install = function (Vue) {
|
|||
const message = JSON.stringify(data)
|
||||
socket.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置定时器发送心跳消息
|
||||
setInterval(() => {
|
||||
sendHeartbeat()
|
||||
}, 5000)
|
||||
|
||||
}
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
console.log('接收到服务器发送的消息:', JSON.parse(event.data))
|
||||
|
||||
// 处理服务器返回的心跳响应
|
||||
const response = JSON.parse(event.data)
|
||||
if (response.type === 'heartbeat') {
|
||||
console.log('收到服务器的心跳响应')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
socket.onclose = () => {
|
||||
|
@ -42,8 +63,10 @@ websocketPlugin.install = function (Vue) {
|
|||
Vue.mixin({
|
||||
beforeDestroy () {
|
||||
this.$socket.close()
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export default websocketPlugin
|
||||
export default websocketPlugin;
|
||||
|
||||
|
|
Loading…
Reference in New Issue