Skip to content

Commit fa17320

Browse files
committed
fix: use script setup in ws.vue
1 parent 82a9a1f commit fa17320

File tree

1 file changed

+32
-45
lines changed

1 file changed

+32
-45
lines changed

playground/pages/ws.vue

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
1-
<script>
1+
<script setup lang="ts">
22
import { onBeforeUnmount, onMounted, ref } from 'vue'
33
4-
export default {
5-
setup() {
6-
let ws
7-
const message = ref('')
4+
let ws: WebSocket
5+
const message = ref('')
86
9-
const initWebSocket = () => {
10-
ws = new WebSocket('/_ws')
7+
function initWebSocket() {
8+
ws = new WebSocket('/_ws')
119
12-
ws.onopen = () => {
13-
console.log('WebSocket connection opened')
14-
}
10+
ws.onopen = () => {
11+
console.log('WebSocket connection opened')
12+
}
1513
16-
ws.onmessage = (event) => {
17-
message.value = event.data
18-
console.log('Message received:', event.data)
19-
}
14+
ws.onmessage = (event) => {
15+
message.value = event.data
16+
console.log('Message received:', event.data)
17+
}
2018
21-
ws.onerror = (error) => {
22-
console.error('WebSocket error:', error)
23-
}
19+
ws.onerror = (error) => {
20+
console.error('WebSocket error:', error)
21+
}
2422
25-
ws.onclose = () => {
26-
console.log('WebSocket connection closed')
27-
}
28-
}
29-
30-
const sendMessage = () => {
31-
if (ws && ws.readyState === WebSocket.OPEN) {
32-
ws.send('ping')
33-
}
34-
}
23+
ws.onclose = () => {
24+
console.log('WebSocket connection closed')
25+
}
26+
}
3527
36-
onMounted(() => {
37-
initWebSocket()
38-
})
28+
function sendMessage() {
29+
if (ws && ws.readyState === WebSocket.OPEN) {
30+
ws.send('ping')
31+
}
32+
}
3933
40-
onBeforeUnmount(() => {
41-
if (ws) {
42-
ws.close()
43-
}
44-
})
34+
onMounted(() => {
35+
initWebSocket()
36+
})
4537
46-
return {
47-
message,
48-
sendMessage,
49-
}
50-
},
51-
}
38+
onBeforeUnmount(() => {
39+
if (ws) {
40+
ws.close()
41+
}
42+
})
5243
</script>
5344

5445
<template>
@@ -62,7 +53,3 @@ export default {
6253
</div>
6354
</div>
6455
</template>
65-
66-
<style scoped>
67-
/* Add your styles here */
68-
</style>

0 commit comments

Comments
 (0)