邓洁 : socket

This commit is contained in:
邓洁
2023-12-08 22:36:52 +08:00
parent a0b12dcbdc
commit 4afc8552df

View File

@@ -9,7 +9,7 @@
<el-button type="primary" @click="closeSocket">关闭连接</el-button> <el-button type="primary" @click="closeSocket">关闭连接</el-button>
<el-button type="primary" @click="handleClear" style="float: right">清除</el-button> <el-button type="primary" @click="handleClear" style="float: right">清除</el-button>
<div class="socket-box"> <div class="socket-box">
<div v-for="item in data" ref="child"> <div v-for="item in dataList" ref="child">
<div v-if="item.type == 3"> <div v-if="item.type == 3">
<span style="color: #007bff">server send:</span> <span style="color: #007bff">server send:</span>
<div>{{ item.cmd }}</div> <div>{{ item.cmd }}</div>
@@ -19,6 +19,10 @@
<div>{{ item.cmd }}</div> <div>{{ item.cmd }}</div>
</div> </div>
<div v-if="item.type == 1">
<span style="color: red"> client send:</span>
<div>{{ item.cmd }}</div>
</div>
</div> </div>
</div> </div>
<div style="display: flex"> <div style="display: flex">
@@ -43,12 +47,13 @@ const number = ref('')
let send = { let send = {
type: "ping" type: "ping"
} }
let data = ref([]) let dataList = ref([])
let customerSend = ref([]) let customerSend = ref([])
let token = getToken(); let token = getToken();
const child = ref(); const child = ref();
const clientSend=ref()
watch( watch(
() => data.value, () => dataList.value,
(newVal) => { (newVal) => {
nextTick(() => { nextTick(() => {
child.value[newVal.length - 1].scrollIntoView(); // 关键代码 child.value[newVal.length - 1].scrollIntoView(); // 关键代码
@@ -59,50 +64,36 @@ watch(
} }
); );
let socket = reactive('') let socket = reactive('')
const handleSend=()=>{ const handleSend = () => {
console.log('JSON.stringify(number.value))',JSON.stringify(number.value))
let data = { let data = {
type: 1, type: 1,
cmd : number.value cmd: number.value
} }
socket.send(JSON.stringify(data)) socket.send(JSON.stringify(data))
dataList.value.push(data)
} }
const initWebSocket = () => { const initWebSocket = () => {
// let wsUrl = `ws://192.168.31.175:9000/debug/${token}/${serialNumber.value}` // let wsUrl = `ws://192.168.31.175:9000/debug/${token}/${serialNumber.value}`
let wsUrl = `ws://web-tunnel.feashow.com/api/wstunnel/debug/${token}/${serialNumber.value}` let wsUrl = `ws://web-tunnel.feashow.com/api/wstunnel/debug/${token}/${serialNumber.value}`
console.log(wsUrl) console.log(wsUrl)
socket = new WebSocket(wsUrl) socket = new WebSocket(wsUrl)
// 2. ws.send()给服务器发送信息
//连接发生错误的回调方法 //连接发生错误的回调方法
socket.onerror = function () { socket.onerror = function () {
console.log("ws连接发生错误"); console.log("ws连接发生错误");
}; };
//连接成功建立的回调方法 //连接成功建立的回调方法
socket.onopen = function () { socket.onopen = function () {
// let authInfo = {
// // token: getToken(),
// type: "auth",
// cluster: "notice"
// }
// socket.send(JSON.stringify(authInfo))
console.log("ws连接成功"); console.log("ws连接成功");
} }
//接收到消息的回调方法 //接收到消息的回调方法
socket.onmessage = function (event) { socket.onmessage = function (event) {
data.value.push(JSON.parse(event.data)) dataList.value.push(JSON.parse(event.data))
// else {
// customerSend.value.push(JSON.parse(event.data))
// }
console.log("服务器返回的信息: ", JSON.parse(event.data)); console.log("服务器返回的信息: ", JSON.parse(event.data));
} }
//连接关闭的回调方法 //连接关闭的回调方法
socket.onclose = function () { socket.onclose = function () {
// initWebSocket()
console.log("ws连接关闭"); console.log("ws连接关闭");
} }
// setInterval(() => {
// socket.send(JSON.stringify(send))
// }, 30000)
} }
const closeSocket = () => { const closeSocket = () => {
socket.close(); socket.close();
@@ -112,9 +103,8 @@ const handleLogout = () => {
router.push('/login') router.push('/login')
} }
const handleClear = () => { const handleClear = () => {
data.value = [] dataList.value = []
} }
// initWebSocket()
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>