WebSocket 使用实例
WebSocket 使用实例:
if ('WebSocket' in window) {
var params = {
sid: sid,
method: self.selectValue,
barId: self.$parent.rowId,
target: self.ruleForm.ip,
number: self.ruleForm.number,
seq: self.seq
}
var sendMsg = JSON.stringify(params)
var socket = new WebSocket(socketUrl + '/ws/' + sid)
socket.onopen = function () {
socket.send(sendMsg)
}
socket.onmessage = function (msg) {
// console.log(msg.data)
self.dialogLoading = false
if (JSON.parse(msg.data).seq == self.seq) {
resultArr.push(JSON.parse(msg.data).result)
self.$nextTick(function () {
self.resultArr = resultArr.join(' ').split('Enter')
if (self.resultArr.length == self.ruleForm.number) {
self.startStatus = true
}
})
}
}
socket.onclose = function () {
console.log('连接正在关闭')
}
socket.onerror = function () {
self.stop()
self.dialogLoading = false
self.$message({
message: 'websocket 连接失败',
duration: 2000,
type: 'error'
})
}
} else {
self.$message({
message: '当前浏览器不支持websock',
duration: 2000,
type: 'error'
})
}
上一篇:Git 删除本地分支和远程分支
下一篇:高德地图使用实例一
文章
总共 0 条评论