赞
踩
为了获取实时数据,前端需要和后端保持通信,HTTP 协议只能是客户端向服务器发出请求,服务器返回查询结果。这种单向请求的特点,注定了如果服务器有连续的状态变化,客户端要获知就非常麻烦。我们只能使用"轮询":每隔一段时候,就发出一个询问,了解服务器有没有新的信息。
websocket是前后端交互的长连接,在使用的过程中,遇到弱网或者网络暂时断连的情况时,服务端并没有触发onclose的事件,客户端也无法得知当前连接是否已经断开,服务端会继续向客户端发送多余链接,并且这些数据还会丢失。因此为了保证连接的可持续性和稳定性,我们需要有一套机制来检测客户端和服务端是否处于正常连接状态,websocket心跳重连就应运而生。
实例对象的onopen属性,用于指定连接成功后的回调函数。
- ws.onopen = function () {
- ws.send('Hello Server!');
- }
如果要指定多个回调函数,可以使用addEventListener
方法
ws.addEventListener('open', function (event) {ws.send('Hello Server!');});
readyState
属性返回实例对象的当前状态,共有四种。
CONNECTING:值为0,表示正在连接。
OPEN:值为1,表示连接成功,可以通信了。
CLOSING:值为2,表示连接正在关闭。
CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
实例对象的onerror
属性,用于指定报错时的回调函数。
实例对象的onclose
属性,用于指定连接关闭后的回调函数。
实例对象的send()
方法用于向服务器发送数据。
实例对象的bufferedAmount
属性,表示还有多少字节的二进制数据没有发送出去。它可以用来判断发送是否结束。
- <html>
- <head>
- <meta charset="utf-8">
- <title>WebSocket Demo</title>
- </head>
- <body>
- <script type="text/javascript">
- // var ws = new WebSocket("wss://echo.websocket.org");
- /*
- ws.onerror = function(e) {
- console.log('已关闭');
- };
- ws.onopen = function(e) {
- console.log('握手成功');
- ws.send('123456789');
- }
- ws.onclose = function() {
- console.log('已关闭');
- }
- ws.onmessage = function(e) {
- console.log('收到消息');
- console.log(e);
- }
- */
-
- var lockReconnect = false;//避免重复连接
- var wsUrl = "wss://echo.websocket.org";
- var ws;
- var tt;
- function createWebSocket() {
- try {
- ws = new WebSocket(wsUrl);
- init();
- } catch(e) {
- console.log('catch');
- reconnect(wsUrl);
- }
- }
- function init() {
- ws.onclose = function () {
- console.log('链接关闭');
- reconnect(wsUrl);
- };
- ws.onerror = function() {
- console.log('发生异常了');
- reconnect(wsUrl);
- };
- ws.onopen = function () {
- //心跳检测重置
- heartCheck.start();
- };
- ws.onmessage = function (event) {
- //拿到任何消息都说明当前连接是正常的
- console.log('接收到消息');
- heartCheck.start();
- }
- }
- function reconnect(url) {
- if(lockReconnect) {
- return;
- };
- lockReconnect = true;
- //没连接上会一直重连,设置延迟避免请求过多
- tt && clearTimeout(tt);
- tt = setTimeout(function () {
- createWebSocket(url);
- lockReconnect = false;
- }, 4000);
- }
- //心跳检测
- var heartCheck = {
- timeout: 3000,
- timeoutObj: null,
- serverTimeoutObj: null,
- start: function(){
- console.log('start');
- var self = this;
- this.timeoutObj && clearTimeout(this.timeoutObj);
- this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
- this.timeoutObj = setTimeout(function(){
- //这里发送一个心跳,后端收到后,返回一个心跳消息,
- console.log('55555');
- ws.send("123456789");
- self.serverTimeoutObj = setTimeout(function() {
- console.log(111);
- console.log(ws);
- ws.close();
- // createWebSocket();
- }, self.timeout);
-
- }, this.timeout)
- }
- }
- createWebSocket(wsUrl);
- </script>
- </body>
- </html>

- function createWebSocket() {
- try {
- ws = new WebSocket(wsUrl);
- init();
- } catch(e) {
- console.log('catch');
- reconnect(wsUrl);
- }
- }
- function init() {
- ws.onclose = function () {
- console.log('链接关闭');
- reconnect(wsUrl);
- };
- ws.onerror = function() {
- console.log('发生异常了');
- reconnect(wsUrl);
- };
- ws.onopen = function () {
- //心跳检测重置
- heartCheck.start();
- };
- ws.onmessage = function (event) {
- //拿到任何消息都说明当前连接是正常的
- console.log('接收到消息');
- heartCheck.start();
- }
- }

- var lockReconnect = false;//避免重复连接
- function reconnect(url) {
- if(lockReconnect) {
- return;
- };
- lockReconnect = true;
- //没连接上会一直重连,设置延迟避免请求过多
- tt && clearTimeout(tt);
- tt = setTimeout(function () {
- createWebSocket(url);
- lockReconnect = false;
- }, 4000);
- }
- //心跳检测
- var heartCheck = {
- timeout: 3000,
- timeoutObj: null,
- serverTimeoutObj: null,
- start: function(){
- console.log('start');
- var self = this;
- this.timeoutObj && clearTimeout(this.timeoutObj);
- this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
- this.timeoutObj = setTimeout(function(){
- //这里发送一个心跳,后端收到后,返回一个心跳消息,
- //onmessage拿到返回的心跳就说明连接正常
- console.log('55555');
- ws.send("123456789");
- self.serverTimeoutObj = setTimeout(function() {
- console.log(111);
- console.log(ws);
- ws.close();
- // createWebSocket();
- }, self.timeout);
-
- }, this.timeout)
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。