<template>
<div>
<el-button @click="clear()">清空</el-button>
<el-button @click="close()">关闭</el-button>
<el-button @click="start()">启动</el-button>
<el-button @click="send()">发送</el-button>
<div class="widget-body" style="height: 500px;">
<el-form ref="form3" :model="form3" label-width="0px">
<el-form-item label="" style="height: 90px;">
<el-input type="textarea" v-model="form3.remark" :rows="20"
placeholder="json"></el-input>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
websock: null,
form3: {
remark: ''
}
}
},
created() {
// this.websock = new WebSocket('ws://192.168.122.136:5414')
// this.form3.remark = this.form3.remark + '连接状态:' + this.websock.readyState + ' \r\n'
// this.websock.onopen = this.websocketonopen
// this.websock.onmessage = this.websocketonmessage
// this.websock.onerror = this.websocketerror
// this.websock.onclose = this.websocketclose
},
methods: {
clear() {
this.form3.remark = ''
},
close() {
},
start() {
// 需判断不重复启动,启动一次,启动多次会收到多条返回值 192.168.122.136 127.0.0.1
this.websock = new WebSocket('ws://192.168.122.136:5414')
this.form3.remark = this.form3.remark + '连接状态:' + this.websock.readyState + ' \r\n'
this.websock.onopen = this.websocketonopen
this.websock.onmessage = this.websocketonmessage
this.websock.onerror = this.websocketerror
this.websock.onclose = this.websocketclose
},
send() {
let actions = {'hardwaretype':'0','modeltype':'1000','opercode': 'zhangc','opername': 'zc测试','servicecode': 'routerurl','serviceinfo':{'infos':'hello'}}
this.form3.remark = this.form3.remark + '发送json:' + JSON.stringify(actions) + ' \r\n'
this.websocketsend(JSON.stringify(actions))
},
// 连接建立之后执行send方法发送数据
websocketonopen() {
this.form3.remark = this.form3.remark + 'open' + ' \r\n'
// let actions = {'reqtype': '1001'}
// this.form3.remark = this.form3.remark + '发送json:' + JSON.stringify(actions) + ' \r\n'
// this.websocketsend(JSON.stringify(actions))
},
// 数据发送
websocketsend(agentData) {
this.websock.send(agentData)
// this.form3.remark = this.form3.remark + '发送\r\n '
},
// 数据接收
websocketonmessage(e) {
const redata = JSON.parse(e.data)
this.form3.remark = this.form3.remark + '接收json:' + JSON.stringify(redata) + ' \r\n'
},
websocketerror(e) {
this.form3.remark = this.form3.remark + 'error\r\n '
},
websocketclose(e) {
this.form3.remark = this.form3.remark + 'connection closed (' + e.code + ')\r\n '
}
}
}
</script>
c#websocket服务与vue通讯
需积分: 50 71 浏览量
2018-11-28
13:21:40
上传
评论
收藏 3KB ZIP 举报
编码15年
- 粉丝: 1
- 资源: 4
最新资源
- 软件仿真多机串行通信.doc
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈