- websocket () {let ws = new WebSocket('ws://localhost:8080')
- ws.onopen = () => {
- // Web Socket 已连接上, 使用 send() 方法发送数据
- ws.send('Holle')
- console.log('数据发送中...')
- }
- ws.onmessage = evt => {
- // console.log('数据已接收...')
- }
- ws.onclose = function () {
- // 关闭 websocket
- console.log('连接已关闭...')
- }
- // 路由跳转时结束 websocket 链接
- this.$router.afterEach(function () {
- ws.close()
- })
- }
来源: http://www.qdfuns.com/article/29986/a1bc4523ac6e4c1ed00d0e74da4cb36d.html