<template
>
<div
class="test">
</div
>
</template
>
<script
>
export default {
name
: 'test',
data() {
return {
websocket
: null,
}
},
created() {
this.initWebSocket()
},
destroyed() {
this.websocket
.close()
},
methods
: {
initWebSocket() {
const uri
= 'ws://127.0.0.1:5000/ws/message'
this.websocket
= new WebSocket(uri
)
this.websocket
.onmessage
= this.websocketOnMessage
this.websocket
.onopen
= this.websocketOnOpen
this.websocket
.onerror
= this.websocketOnError
this.websocket
.onclose
= this.websocketClose
},
websocketOnOpen() {
console
.log('建立连接')
},
websocketOnError() {
this.initWebSocket()
},
websocketOnMessage(e
) {
console
.log(e
.data
)
this.percent
= Number(e
.data
)
},
websocketSend(data
) {
this.websocket
.send(data
)
},
websocketClose(e
) {
console
.log('断开连接', e
)
},
},
}
</script
>
<style
>
</style
>
转载请注明原文地址:https://tech.qufami.com/read-9321.html