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 },
0 commit comments