-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
82 lines (79 loc) · 2.79 KB
/
index.html
File metadata and controls
82 lines (79 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moodrain WebSocket Test</title>
<link rel="stylesheet" href="https://s1.moodrain.cn/lib/element-ui/index.css">
</head>
<body>
<div id="app">
<div style="width: 100%;text-align: center;">
<el-button @click="newConn" type="primary">New Connection</el-button>
</div>
<el-divider></el-divider>
<el-card v-for="conn in conns" key:="conn.id" style="margin: 5px auto;width: 50%">
<el-input v-model="conn.input">
<template slot="append">
<el-button @click="send(conn)">Send</el-button>
<el-button @click="close(conn)">Close</el-button>
</template>
</el-input>
<el-input :value="conn.msgs.join('\n')" readonly type="textarea"></el-input>
</el-card>
</div>
<script src="https://s1.moodrain.cn/lib/vue/index.js"></script>
<script src="https://s1.moodrain.cn/lib/element-ui/index.js"></script>
<script>
const url = 'ws://127.0.0.1:8001'
let id = 1
let app = new Vue({
el: '#app',
data() {
return {
conns: [],
}
},
methods: {
newConn() {
let ws = new WebSocket(url)
let conn = {
id,
ws,
msgs: [],
input: '',
}
ws.addEventListener('close', () => {
let index = this.conns.findIndex(c => c == conn)
if (index != -1) {
this.conns.splice(index, 1)
}
})
ws.addEventListener('message', () => {
let index = this.conns.findIndex(c => c == conn)
this.conns[index].msgs.unshift(event.data)
})
this.conns.push(conn)
id++
},
send(conn) {
let input = conn.input
if (!input) {
return
}
conn.input = ''
conn.ws.send(input)
},
close(conn) {
let index = this.conns.findIndex(c => c == conn)
if (index != -1) {
this.conns.splice(index, 1)
}
conn.ws.close()
}
},
})
</script>
</body>
</html>