fix : 修复socket接收数据功能
This commit is contained in:
@@ -7,7 +7,12 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
|
import {useAuthStore} from '@/stores/userstore.js'
|
||||||
import {getToken} from '@/utils/auth'
|
import {getToken} from '@/utils/auth'
|
||||||
|
import {usePermisstionStroe} from '@/stores/permisstion'
|
||||||
|
|
||||||
|
const permisstionStore = usePermisstionStroe()
|
||||||
|
const authStore = useAuthStore()
|
||||||
//聊天消息滚动面板dom
|
//聊天消息滚动面板dom
|
||||||
const recordLeftRef = ref(null);
|
const recordLeftRef = ref(null);
|
||||||
const recordRightRef = ref(null);
|
const recordRightRef = ref(null);
|
||||||
@@ -95,10 +100,16 @@ const scrollToBottom = (scrollbarRef) => {
|
|||||||
const setWsUrl = (url) => {
|
const setWsUrl = (url) => {
|
||||||
return (window.location.protocol === 'http:' ? "ws://" : "wss://") + window.location.host + import.meta.env.VITE_BASE_WSURL + url;
|
return (window.location.protocol === 'http:' ? "ws://" : "wss://") + window.location.host + import.meta.env.VITE_BASE_WSURL + url;
|
||||||
}
|
}
|
||||||
|
const handleLogout = () => {
|
||||||
|
authStore.userLogout()
|
||||||
|
permisstionStore.removeMenu()
|
||||||
|
router.push('/login')
|
||||||
|
}
|
||||||
const initWebSocket = () => {
|
const initWebSocket = () => {
|
||||||
try {
|
try {
|
||||||
// const wsUrl=setWsUrl(`/ws/text/${token}`)
|
// const wsUrl=setWsUrl(`/ws/text/${token}`)
|
||||||
const wsUrl = `ws://frp.feashow.cn:31800/ws/text/${token}`
|
const wsUrl = `ws://frp.feashow.cn:31800/ws/text/${token}`
|
||||||
|
// const wsUrl = `ws://112.19.165.99:20002/ws/text/${token}`
|
||||||
socket = new WebSocket(wsUrl)
|
socket = new WebSocket(wsUrl)
|
||||||
// 2. ws.send()给服务器发送信息
|
// 2. ws.send()给服务器发送信息
|
||||||
//连接发生错误的回调方法
|
//连接发生错误的回调方法
|
||||||
@@ -119,6 +130,13 @@ const initWebSocket = () => {
|
|||||||
socket.onmessage = function (event) {
|
socket.onmessage = function (event) {
|
||||||
let data = JSON.parse(event.data)
|
let data = JSON.parse(event.data)
|
||||||
if (data.type !== 'pong') {
|
if (data.type !== 'pong') {
|
||||||
|
if (data.type === 'error') {
|
||||||
|
handleLogout()
|
||||||
|
return;
|
||||||
|
} else if (data.type === 'failed') {
|
||||||
|
initWebSocket()
|
||||||
|
return;
|
||||||
|
}
|
||||||
console.log("服务器返回的信息: ", data);
|
console.log("服务器返回的信息: ", data);
|
||||||
if (data.type == '1') {
|
if (data.type == '1') {
|
||||||
recordLeftObj.value.content.push(data.content)
|
recordLeftObj.value.content.push(data.content)
|
||||||
@@ -133,7 +151,6 @@ const initWebSocket = () => {
|
|||||||
scrollToBottom(recordRightRef.value);
|
scrollToBottom(recordRightRef.value);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//连接关闭的回调方法
|
//连接关闭的回调方法
|
||||||
@@ -143,7 +160,7 @@ const initWebSocket = () => {
|
|||||||
}
|
}
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
socket.send(JSON.stringify(send))
|
socket.send(JSON.stringify(send))
|
||||||
}, 10000)
|
}, 5000)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
console.log("ws连接失败");
|
console.log("ws连接失败");
|
||||||
|
|||||||
Reference in New Issue
Block a user