<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h1>{{ res }}</h1>
</div>
</template> <script>
export default {
name: 'ws',
data () {
return {
msg: '',
res: {},
lot_id: '000',
ws: null,
wsurl: ''
}
},
created () {
this.initws()
},
mounted () {
this.runws()
},
methods: {
runws () {
let content = 'source=client&lots=' + this.lot_id
if (this.ws.readyState === this.ws.OPEN) {
this.sendmessage(content)
} else if (this.ws.readyState === this.ws.CONNECTING) {
let that = this
setTimeout(function () {
that.sendmessage(content)
}, 300)
} else {
this.initws()
let that = this
setTimeout(function () {
that.sendmessage(content)
}, 500)
}
},
initws () {
this.ws = new WebSocket(this.wsurl)
this.ws.onmessage = this.getmessage
},
getmessage (e) {
this.res = JSON.parse(e.data)
},
sendmessage (content) {
this.ws.send(content)
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

  

在 vue 中使用 WebSocket的更多相关文章

  1. 解决在VUE中绑定WebSocket事件无法传值的问题

    this.webSocket.onerror =(env)=>{ this.onError(env) }; https://jsfiddle.net/dko6g9a4/1/

  2. vue中开发webSocket

    先安装 sockjs-client 和 stompjs npm install sockjs-client npm install stompjs <template> <div&g ...

  3. Vue中使用websocket

    <template>   <div class="test">websocket demo</div></template> < ...

  4. 在vue中如何使用WebSocket 以及nginx代理如何配置WebSocket

    WebSocket WebSocket是一种网络传输协议,可在单个TCP连接上进行全双工通信.浏览器和服务器只需要完成一次握手,两者之间就可以创建持久性的连接,并进行双向数据传输. 浏览器支持情况 现 ...

  5. vue中websoket的使用

    首先安装npm install --save  websocket-heartbeat-js@^1.0.7 在main.js中  引入并挂载全局方法 import WebsocketHeartbeat ...

  6. vue项目使用websocket做聊天项目总结

    一.首先我们先了解一下websocket的使用: 1.创建websocket const ws = new WebSocket("ws://192.168.31.136:9998/ws&qu ...

  7. vue中解决chrome浏览器自动播放音频 和MP3语音打包到线上

    一.vue中解决chrome浏览器自动播放音频 需求 有新订单的时候,页面自动语音提示和弹出提示框: 问题 chrome浏览器在18年4月起,就在桌面浏览器全面禁止了音视频的自动播放功能.严格地来说, ...

  8. vue - Vue中的ajax

    只有在ajax才能找回一点点主场了,vue中的ajax一天整完,内容还行,主要是对axios的运用. 明天按理说要开始vuex了,这个从来都是只耳闻没有眼见过,明天来看看看看是个什么神奇的东西. 一. ...

  9. 【C#】MVC项目中搭建WebSocket服务器

    前言 因为项目需要,前端页面中需要不断向后台请求获取一个及一个以上的状态值.最初的方案是为每个状态值请求都建立一个定时器循环定时发起Ajax请求,结果显而 易见.在HTTP1.1协议中,同一客户端浏览 ...

随机推荐

  1. AliOS Things添加环境变量

    此电脑->属性->高级系统设置->环境变量->系统变量-新建.

  2. Spring 中Controller 获取请求参数的方法笔记

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...

  3. [ 头皮发麻 A1 ] 队内赛3 2020 Ateneo de Manila University DISCS PrO HS Division

    都是英语阅读题 但是本菜鸡就过了一题,直接自闭mmp明天开始起床一版题 传送门 B.Riana and the Blind Date 0是闰年?惊了 后来才知道整除被除数可以为0 闰年的计算方法 \( ...

  4. JNI通过线程c回调java层的函数

    1.参看博客:http://www.jianshu.com/p/e576c7e1c403 Android JNI 篇 - JNI回调的三种方法(精华篇) 2.参看博客: JNI层线程回调Java函数关 ...

  5. Docker搭建Rancher平台

    sudo docker run -d --restart=unless-stopped -p 8080:8080 rancher/server service docker resatrt启动失败   ...

  6. day10,day11—基本数据类型语法

    一.整形 1. base #在16进制中的位置 num = "b" v = int(num, base=16) print(v) #11 2. bit_length() # 1 1 ...

  7. 洛谷 P5683 【[CSPJX2019]道路拆除】

    先用做的暴力,因为n最多才3000嘛,但是后来发现时间复杂度不止\(O\)(\({n}^2\)),然后就放弃了. 讲讲我的暴力+错误思路吧: 把1到s1和s2的最短路算出来,用SPFA,然后用DFS求 ...

  8. Material Component--mdcChipSet使用

    <div mdcChipSet="choice"> <div mdcChip *ngFor="let item of ywDicTypes" ...

  9. CentOS 的数字命令级别

    1    user commands 2    system calls 3    library functions 4    special  files 5     file formats 6 ...

  10. 关于延迟段创建-P1

    文章目录 1 疑问点 2 环境创建 2.1 创建用户 2.2 创建表test 2.3 查看表的段信息 2.4 延迟段创建相关参数 1 疑问点 P1页有句话说道: 在Oracle 11.2.0.3.0以 ...