Skip to content

Commit 6d458c6

Browse files
committed
add: webSocket
1 parent 19fd15d commit 6d458c6

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

admin/src/router/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const routeBase = [
2828
{ path: '/line', name: 'line', component: () => import('@/views/home/line'), hidden: true },
2929
{ path: '/home', component: () => import('@/views/home/home'), hidden: true },
3030
{ path: '/md', name: 'md', component: () => import('@/views/markdown/index'), hidden: true },
31+
{ path: '/websocket', name: 'websocket', component: () => import('@/views/home/websocket'), hidden: true },
3132
{ path: '/echarts', name: 'echarts', component: () => import('@/views/echarts/index'), hidden: true },
3233
// { path: '/index', component: () => import('@/views/home/index'), hidden: true },
3334
{ path: '/login', component: () => import('@/views/login/index'), hidden: true },

admin/src/views/api_excel/index.vue

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@
5555
</template>
5656
</el-table-column>
5757

58+
<el-table-column label="进度条" width="1" align="center" display="none">
59+
<template slot-scope="scope">
60+
<div v-if="scope.row.state === 0">
61+
<el-progress :text-inside="true" :stroke-width="18" :percentage="0"/>
62+
</div>
63+
<div v-else-if="scope.row.state === 1">
64+
<el-progress :text-inside="true" :stroke-width="18" :percentage="1"/>
65+
</div>
66+
<div v-else-if="scope.row.state === 2">
67+
<el-progress :text-inside="true" :stroke-width="18" :percentage="100" status="success"/>
68+
</div>
69+
<div v-else>
70+
<el-progress :text-inside="true" :stroke-width="18" :percentage="50" status="exception"/>
71+
</div>
72+
</template>
73+
</el-table-column>
74+
5875
<el-table-column label="操作" width="200" align="center">
5976
<template slot-scope="scope">
6077
<div>
@@ -125,7 +142,8 @@ export default {
125142
total: 100,
126143
currentpage: 1,
127144
listQuery: { page: 1 },
128-
url: null
145+
url: null,
146+
websock: null
129147
}
130148
},
131149
created() {
@@ -135,6 +153,9 @@ export default {
135153
this.perpage = isNaN(perPage) ? this.perpage : perPage
136154
this.fetchData()
137155
},
156+
destroyed() {
157+
this.websock.close() // 离开路由之后断开 websocket 连接
158+
},
138159
methods: {
139160
startTask(index, row) {
140161
this.$confirm('此操作将开启任务, 是否继续?', '提示', {
@@ -167,7 +188,7 @@ export default {
167188
this.$alert('此操作将开启任务, 是否继续?', '开启任务提醒', {
168189
confirmButtonText: '确定',
169190
center: true,
170-
type: 'success',
191+
type: 'warning',
171192
callback: action => {
172193
if (action === 'confirm') {
173194
startTask(row).then(res => {
@@ -188,6 +209,38 @@ export default {
188209
}
189210
})
190211
},
212+
initWebSocket(id) { // 初始化 weosocket
213+
if ('WebSocket' in window) {
214+
const url = 'ws://127.0.0.1:5200?id=' + id
215+
this.websock = new WebSocket(url)
216+
this.websock.onmessage = this.onmessage
217+
this.websock.onopen = this.onopen
218+
this.websock.onerror = this.onerror
219+
this.websock.onclose = this.close
220+
} else {
221+
// 浏览器不支持 WebSocket,使用 ajax 轮询
222+
console.log('Your browser does not support WebSocket!')
223+
}
224+
},
225+
onopen() { // 连接建立之后执行send方法发送数据
226+
// const actions = { 'id': '7' }
227+
// const rs = this.send(JSON.stringify(actions))
228+
// console.log(rs)
229+
},
230+
onerror() { // 连接建立失败重连
231+
this.initWebSocket()
232+
},
233+
onmessage(e) { // 数据接收
234+
console.log(e.data)
235+
const redata = JSON.parse(e.data)
236+
console.log(redata)
237+
},
238+
send(Data) {
239+
this.websock.send(Data)
240+
},
241+
close(e) { // 关闭
242+
console.log('断开连接', e)
243+
},
191244
download(index, row) {
192245
window.location.href = this.url + row.finish_url
193246
},

admin/src/views/home/websocket.vue

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<template>
2+
<div class="test">1111
3+
<span style="display:block;">点击</span>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'WebSocket',
10+
data() {
11+
return {
12+
websock: null
13+
}
14+
},
15+
created() {
16+
this.initWebSocket()
17+
},
18+
destroyed() {
19+
this.websock.close() // 离开路由之后断开 websocket 连接
20+
},
21+
methods: {
22+
initWebSocket() { // 初始化 weosocket
23+
if ('WebSocket' in window) {
24+
const url = 'ws://127.0.0.1:5200?id=6'
25+
this.websock = new WebSocket(url)
26+
this.websock.onmessage = this.onmessage
27+
this.websock.onopen = this.onopen
28+
this.websock.onerror = this.onerror
29+
this.websock.onclose = this.close
30+
} else {
31+
// 浏览器不支持 WebSocket,使用 ajax 轮询
32+
console.log('Your browser does not support WebSocket!')
33+
}
34+
},
35+
onopen() { // 连接建立之后执行send方法发送数据
36+
// const actions = { 'id': '7' }
37+
// const str = JSON.stringify(actions)
38+
// console.log(str)
39+
// // 数据发送
40+
// const rs = this.send(str)
41+
// console.log(rs)
42+
},
43+
onerror() { // 连接建立失败重连
44+
this.initWebSocket()
45+
},
46+
onmessage(e) { // 数据接收
47+
console.log(e.data)
48+
const redata = JSON.parse(e.data)
49+
console.log(redata)
50+
},
51+
send(Data) {
52+
this.websock.send(Data)
53+
},
54+
close(e) { // 关闭
55+
console.log('断开连接', e)
56+
}
57+
}
58+
}
59+
</script>
60+
61+
<style>
62+
</style>

0 commit comments

Comments
 (0)