提交 cf78ced7 authored 作者: GOD_ZYX's avatar GOD_ZYX

修改 socket

上级 8367fc8c
...@@ -17,18 +17,21 @@ export default { ...@@ -17,18 +17,21 @@ export default {
mounted () { mounted () {
if (window.WebSocket) { if (window.WebSocket) {
try { try {
socket = this.createSocket() socket = this.createSocketClient()
} catch (e) { } catch (e) {
console.log('不支持,websocket') console.log('不支持,websocket')
} }
} }
}, },
methods: { methods: {
createSocket () { createSocketClient () {
let that = this let that = this
let _socket = new WebSocket(window.location.protocol.replace(/http/gi, 'ws') + '//' + window.location.host) // eslint-disable-line let _socket = new WebSocket(window.location.protocol.replace(/http/gi, 'ws') + '//' + window.location.host) // eslint-disable-line
_socket.binaryType = 'arraybuffer' _socket.binaryType = 'arraybuffer'
that.socketHeart && clearInterval(that.socketHeart)
that.socketHeart = that.createSocketHeart()
_socket.onmessage = function (msg) { _socket.onmessage = function (msg) {
if (msg.data instanceof ArrayBuffer) { if (msg.data instanceof ArrayBuffer) {
var str = String.fromCharCode.apply(null, new Uint8Array(msg.data)) var str = String.fromCharCode.apply(null, new Uint8Array(msg.data))
...@@ -44,19 +47,17 @@ export default { ...@@ -44,19 +47,17 @@ export default {
} }
} }
_socket.onopen = function (e) { _socket.onopen = function (e) {
that.socketHeart && clearInterval(that.socketHeart)
that.socketHeart = that.createSocketHeart()
} }
_socket.onclose = function (e) { _socket.onclose = function (e) {
/* Socket连接关闭,则重连 */ /* Socket连接关闭,则重连 */
if (e.target && e.target.readyState === 2) { if (e.target && e.target.readyState === 2) {
socket = that.createSocket() socket = that.createSocketClient()
} }
} }
_socket.onerror = function (e) { _socket.onerror = function (e) {
/* Socket连接出错,则重连 */ /* Socket连接出错,则重连 */
if (e.target && e.target.readyState === 3) { if (e.target && e.target.readyState === 3) {
socket = that.createSocket() socket = that.createSocketClient()
} }
} }
return _socket return _socket
...@@ -83,7 +84,7 @@ export default { ...@@ -83,7 +84,7 @@ export default {
createSocketHeart () { createSocketHeart () {
return setInterval(() => { return setInterval(() => {
if (socket && socket.readyState !== 1) { if (socket && socket.readyState !== 1) {
socket = this.createSocket() socket = this.createSocketClient()
} }
}, 10000) }, 10000)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论