DomCode
已于 2022-12-23 08:55:08 修改
253
收藏
分类专栏: # JavaScript 文章标签: javascript websocket 开发语言
版权
JavaScript
专栏收录该内容
14 篇文章0 订阅
订阅专栏
WebSocket 是 HTML5 开始提供的一种浏览器与服务器间进行全双工通讯的网络技术。 WebSocket 通信协议于 2011 年被 IETF 定为标准 RFC 6455,WebSocketAPI 被 W3C 定为标准。
在 WebSocket API 中,浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。
现在,很多网站为了实现即时通讯 (real-time),所用的技术都是轮询 (polling)。轮询是在特定的的时间间隔 (time interval)(如每 1 秒),由浏览器对服务器发出 HTTP request,然后由服务器返回最新的数据给客服端的浏览器。这种传统的 HTTP request d 的模式带来很明显的缺点 – 浏览器需要不断的向服务器发出请求 (request),然而 HTTP request 的 header 是非常长的,里面包含的数据可能只是一个很小的值,这样会占用很多的带宽。
而最比较新的技术去做轮询的效果是 Comet – 用了 AJAX。但这种技术虽然可达到全双工通信,但依然需要发出请求 (reuqest)。
在 WebSocket API,浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。在此 WebSocket 协议中,为我们实现即使服务带来了两大好处:
Header
互相沟通的 Header 是很小的 - 大概只有 2 Bytes
Server Push
服务器可以主动传送数据给客户端
1,建立webscoket.js,进行封装
import store from '../store/index.js'// 不引用一下的话,vuex存储会报错
let webpackIp = 'ws://1111111111111111'
let wsConnectionOrder = {
$wsOrder: null,
lockReturn: false,
timeout: 60 * 1000 * 5,
timeoutObj: null,
timeoutNum: null,
// 初始化webSocket长连接
initWebSocket: function () {
let user_id = JSON.parse(localStorage.getItem('user_id'))
console.log('webpackid有了么----' + user_id)
let wsuri = webpackIp + '?channel=' + user_id
this.$wsOrder = new WebSocket(wsuri)
this.$wsOrder.onopen = this.wsOpen
this.$wsOrder.onclose = this.wsClose
this.$wsOrder.onmessage = this.wsMsg
this.$wsOrder.onerror = this.wsError
},
// 打开websocket
wsOpen: function (e) {
// 开始websocket心跳
console.log('-----------------------------建立ghzchcgsdjkcgsdku-----------------------------------')
wsConnectionOrder.startWsHeartbeat()
console.log('orderws success')
},
wsClose: function (e) {
wsConnectionOrder.reconnect()
},
wsMsg: function (e) {
console.log('----------------------接收ghzchcgsdjkcgsdku------------------------')
console.log(e)
// 服务端发送来的消息存到vuex(先存储消息再重置心跳)
store.commit('scoketData', {
scoket: e.data
})
// 每次接收到服务端消息后 重置websocket心跳
wsConnectionOrder.resetHeartbeat()
},
wsError: function (e) {
wsConnectionOrder.reconnect()
},
// 重启websocket
reconnect: function () {
console.log('重启函数')
let _this = this
if (_this.lockReturn) {
return
}
_this.lockReturn = true
_this.timeoutNum && clearTimeout(_this.timeoutNum)
_this.timeoutNum = setTimeout(function () {
_this.initWebSocket()
_this.lockReturn = false
}, 1000)
},
// 开启websocket
startWsHeartbeat: function () {
let _this = this
_this.timeoutObj && clearInterval(_this.timeoutObj)
_this.timeoutObj = setInterval(function () {
// 发送消息给服务端
_this.$wsOrder.send('')
// 判断websocket当前状态
if (_this.$wsOrder.readyState !== 1) {
_this.reconnect()
}
}, _this.timeout)
},
// 重置websocket心跳
resetHeartbeat: function () {
let _this = this
clearInterval(_this.timeoutObj)
_this.startWsHeartbeat()
}
}
// 抛出websocket对象
export default {
wsConnectionOrder
}
// import { store } from '../store/index.js'
// let webpackIp = 'ws://10.31.0.184:5679'
// // let webpackIp = 'ws://10.31.0.21:5678'
// let scoket = {
// reconnect () { // 重新连接
// var that = this
// if (that.lockReconnect) {
// return
// }
// that.lockReconnect = true
// // 没连接上会一直重连,设置延迟避免请求过多
// that.timeoutnum && clearTimeout(that.timeoutnum)
// that.timeoutnum = setTimeout(function () {
// // 新连接
// that.initWebSocket()
// that.lockReconnect = false
// }, 5000)
// },
// initWebSocket () { // 初始化weosocket
// let wsuri = ''
// let user_id = JSON.parse(localStorage.getItem('user_id'))
// console.log('webpackid有了么----' + user_id)
// wsuri = webpackIp + '?channel=' + user_id
// console.log(wsuri)
// this.websock = new WebSocket(wsuri)
// this.websock.onopen = this.websocketonopen
// this.websock.onmessage = this.websocketonmessage
// this.websock.onerror = this.websocketonerror
// },
// websocketonopen () { // 连接建立之后执行send方法发送数据
// console.log('-----------------------------建立ghzchcgsdjkcgsdku-----------------------------------')
// // 开启心跳
// // this.start()
// // let user_id = JSON.parse(localStorage.getItem('user_id'))
// // this.websocketsend(JSON.stringify(user_id))
// },
// websocketonerror () { // 连接建立失败重连
// this.initWebSocket()
// // 重连
// // this.reconnect()
// },
// websocketonmessage (e) { // 数据接收
// console.log(this)
// let that = this
// console.log(that)
// console.log('-----------------------------接收ghzchcgsdjkcgsdku-----------------------------------')
// console.log(e)
// store.commit('scoketData', {
// scoket: e
// })
// },
// websocketsend (Data) { // 数据发送
// console.log('-----------------------------发送ghzchcgsdjkcgsdku-----------------------------------')
// this.websock.send(Data)
// },
// websocketclose (e) { // 关闭
// console.log('断开连接', e)
// },
// start () {
// // console.log('开启心跳');
// const self = this
// let _num = 3
// self.timeoutObj && clearTimeout(self.timeoutObj)
// self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj)
// self.timeoutObj = setTimeout(function () {
// // 这里发送一个心跳,后端收到后,返回一个心跳消息,
// if (self.websock.readyState === 1) { // 如果连接正常
// // console.log(self.websock);
// self.websock.send(heartCheck)
// } else { // 否则重连
// self.reconnect()
// }
// self.serverTimeoutObj = setTimeout(function () {
// // 超时关闭
// _num--
// // 计算答复的超时次数
// if (_num === 0) {
// self.websocketclose()
// }
// }, self.timeout)
// }, self.timeout)
// },
// reset () {
// // console.log('重置心跳');
// var that = this
// // 清除时间
// clearTimeout(that.timeoutObj)
// clearTimeout(that.serverTimeoutObj)
// // 重启心跳
// that.start()
// }
// }
// export default {
// scoket
// }
// import { store } from '../store/index.js'
// import store from '../store/index.js'
// let Socket = ''
// let setIntervalWesocketPush = null
// /**
// * 建立websocket连接
// * @param {string} url ws地址
// */
// const initWebSocket = url => {
// Socket && Socket.close()
// if (!Socket) {
// console.log('建立websocket连接')
// let webpackIp = 'ws://10.31.0.184:5679'
// let user_id = JSON.parse(localStorage.getItem('user_id'))
// let url = webpackIp + '?channel=' + user_id
// Socket = new WebSocket(url)
// Socket.onopen = onopenWS
// Socket.onmessage = onmessageWS
// Socket.onerror = onerrorWS
// Socket.onclose = oncloseWS
// } else {
// console.log('websocket已连接')
// }
// }
// /** 打开WS之后发送心跳 */
// const onopenWS = () => {
// console.log('-----------------------------建立ghzchcgsdjkcgsdku-----------------------------------')
// sendPing()
// }
// /** 连接失败重连 */
// const onerrorWS = () => {
// Socket.close()
// clearInterval(setIntervalWesocketPush)
// console.log('连接失败重连中')
// if (Socket.readyState !== 3) {
// Socket = null
// initWebSocket()
// }
// }
// /** WS数据接收统一处理 */
// const onmessageWS = e => {
// console.log('-----------------------------接收ghzchcgsdjkcgsdku-----------------------------------')
// console.log(this)
// console.log(e)
// store.commit('scoketData', {
// scoket: e.data
// })
// }
// /**
// * 发送数据但连接未建立时进行处理等待重发
// * @param {any} message 需要发送的数据
// */
// const connecting = message => {
// setTimeout(() => {
// if (Socket.readyState === 0) {
// connecting(message)
// } else {
// Socket.send(JSON.stringify(message))
// }
// }, 1000)
// }
// /**
// * 发送数据
// * @param {any} message 需要发送的数据
// */
// const sendWSPush = message => {
// if (Socket !== null && Socket.readyState === 3) {
// Socket.close()
// initWebSocket()
// } else if (Socket.readyState === 1) {
// Socket.send(JSON.stringify(message))
// } else if (Socket.readyState === 0) {
// connecting(message)
// }
// }
// /** 断开重连 */
// const oncloseWS = () => {
// clearInterval(setIntervalWesocketPush)
// console.log('websocket已断开....正在尝试重连')
// if (Socket.readyState !== 2) {
// Socket = null
// initWebSocket()
// }
// }
// /** 发送心跳
// * @param {number} time 心跳间隔毫秒 默认5000
// * @param {string} ping 心跳名称 默认字符串ping
// */
// const sendPing = (time = 5000, ping = 'ping') => {
// clearInterval(setIntervalWesocketPush)
// Socket.send(ping)
// setIntervalWesocketPush = setInterval(() => {
// Socket.send(ping)
// }, time)
// }
// export default {
// initWebSocket
// }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
2,main.js中引入
import wsConnectionOrder from './api/webscoket'
Vue.prototype.$scoket = wsConnectionOrder
1
2
3,在需要使用的页面,需要调用哪个方法,直接调用
this.$scoket.wsConnectionOrder.initWebSocket()
————————————————
版权声明:本文为CSDN博主「DomCode」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43094619/article/details/128408322
联系客服