Merge pull request 'fix : 修复socket接收数据功能' (#98) from dd into master

Reviewed-on: http://git.feashow.cn/feashow/SmartOpsWeb/pulls/98
This commit is contained in:
2024-09-23 13:43:39 +00:00

View File

@@ -7,7 +7,12 @@
</template>
<script setup>
import {useAuthStore} from '@/stores/userstore.js'
import {getToken} from '@/utils/auth'
import {usePermisstionStroe} from '@/stores/permisstion'
const permisstionStore = usePermisstionStroe()
const authStore = useAuthStore()
//聊天消息滚动面板dom
const recordLeftRef = ref(null);
const recordRightRef = ref(null);
@@ -95,10 +100,16 @@ const scrollToBottom = (scrollbarRef) => {
const setWsUrl = (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 = () => {
try {
// const wsUrl=setWsUrl(`/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)
// 2. ws.send()给服务器发送信息
//连接发生错误的回调方法
@@ -119,6 +130,13 @@ const initWebSocket = () => {
socket.onmessage = function (event) {
let data = JSON.parse(event.data)
if (data.type !== 'pong') {
if (data.type === 'error') {
handleLogout()
return;
} else if (data.type === 'failed') {
initWebSocket()
return;
}
console.log("服务器返回的信息: ", data);
if (data.type == '1') {
recordLeftObj.value.content.push(data.content)
@@ -133,7 +151,6 @@ const initWebSocket = () => {
scrollToBottom(recordRightRef.value);
})
}
}
}
//连接关闭的回调方法
@@ -143,7 +160,7 @@ const initWebSocket = () => {
}
setInterval(() => {
socket.send(JSON.stringify(send))
}, 10000)
}, 5000)
} catch (e) {
console.log(e)
console.log("ws连接失败");