Browse Source

修改ws

master
liaoxinyu 1 month ago
parent
commit
5f6e33528b
  1. 298
      api/Socket.js
  2. 31
      static/chart_main/sevencoin.js
  3. 326
      static/chart_main/ws.js

298
api/Socket.js

@ -1,5 +1,186 @@
// class Socket {
// constructor(link) {
// // 初始化socket
// if (link.constructor === WebSocket) {
// this.socket = link;
// } else {
// this.socket = new WebSocket(link);
// }
// // this.socket.binaryType = 'arraybuffer';
// this.doOpen();
// // 连接状态的标识符
// this.readyState = this.socket.readyState;
// // 订阅/发布模型
// this._events = {
// // 订阅的事件 : 发布的方法
// };
// // 定时验证的标识符
// this.heartBeatTimer = null;
// }
// // 执行socket并发布事件
// doOpen() {
// this.afterOpenEmit = [];
// // 执行socket连接 并初始化验证请求
// this.socket.addEventListener("open", evt => this.onOpen(evt));
// // 接收socket数据
// this.socket.addEventListener("message", evt => this.onMessage(evt));
// // 关闭socket连接
// this.socket.addEventListener("close", evt => this.onClose(evt));
// // 请求发生错误
// this.socket.addEventListener("error", err => this.onError(err));
// }
// // 发布后通知订阅者
// Notify(entry) {
// // 检查是否有订阅者 返回队列
// const cbQueue = this._events[entry.Event];
// if (cbQueue && cbQueue.length) {
// for (let callback of cbQueue) {
// if (callback instanceof Function) callback(entry.Data);
// }
// }
// }
// // 请求数据的方法
// onOpen(evt) {
// // 每隔20s检查连接
// // this.heartBeatTimer = setInterval(() => this.send({
// // 'cmd': 'ping',
// // 'args': ''
// // }), 20000);
// // 通知订阅
// this.Notify({Event: 'open', Data : evt});
// }
// /**
// * 订阅所有的数据
// * @param {array|object} datas 订阅参数集合
// */
// send(datas) {
// if (datas.constructor != Array) {
// datas = [datas];
// }
// for (let item of datas) {
// this.socket.send(JSON.stringify(item));
// }
// }
// onMessage(evt) {
// try {
// // 解析推送的数据
// const data = JSON.parse(evt.data);
// // 通知订阅者
// this.Notify({
// Event: 'message',
// Data: data
// });
// } catch (err) {
// console.error(' >> Data parsing error:', err);
// // 通知订阅者
// this.Notify({
// Event: 'error',
// Data: err
// });
// }
// }
// // 添加事件监听
// on(name, handler) {
// this.subscribe(name, handler);
// }
// // 取消订阅事件
// off(name, handler) {
// this.unsubscribe(name, handler);
// }
// // 订阅事件的方法
// subscribe(name, handler) {
// if (this._events[name]) {
// this._events[name].push(handler); // 追加事件
// } else {
// this._events[name] = [handler]; // 添加事件
// }
// }
// // 取消订阅事件
// unsubscribe(name, handler) {
// let start = this._events[name].findIndex(item => item === handler);
// // 删除该事件
// this._events[name].splice(start, 1);
// }
// checkOpen() {
// return this.readyState >= 2;
// }
// onClose(evt) {
// this.Notify({Event: 'close', Data : evt});
// }
// onError(err) {
// this.Notify({Event: 'error', Data : err});
// }
// emit(data) {
// return new Promise((resolve) => {
// this.send(JSON.stringify(data));
// this.on('message', function (data) {
// resolve(data);
// });
// });
// }
// doClose() {
// this.socket.close();
// }
// destroy() {
// if (this.heartBeatTimer) {
// clearInterval(this.heartBeatTimer);
// this.heartBeatTimer = null;
// }
// this.doClose();
// this._events = {};
// this.readyState = 0;
// this.socket = null;
// }
// }
// export default Socket
class Socket {
constructor(link) {
// 保存连接地址,为了后面的断线重连用
this.link = link;
// 初始化socket
if (link.constructor === WebSocket) {
this.socket = link;
@ -7,46 +188,38 @@ class Socket {
this.socket = new WebSocket(link);
}
// this.socket.binaryType = 'arraybuffer';
// 订阅/发布模型
this._events = {};
// 定时验证的标识符 (心跳)
this.heartBeatTimer = null;
// 重连相关的标识符
this.reconnectTimer = null;
this.isReconnecting = false;
this.doOpen();
// 连接状态的标识符
this.readyState = this.socket.readyState;
// 订阅/发布模型
this._events = {
// 订阅的事件 : 发布的方法
};
// 定时验证的标识符
this.heartBeatTimer = null;
}
// 执行socket并发布事件
doOpen() {
this.afterOpenEmit = [];
// 执行socket连接 并初始化验证请求
this.socket.addEventListener("open", evt => this.onOpen(evt));
// 接收socket数据
this.socket.addEventListener("message", evt => this.onMessage(evt));
// 关闭socket连接
this.socket.addEventListener("close", evt => this.onClose(evt));
// 请求发生错误
this.socket.addEventListener("error", err => this.onError(err));
}
// 发布后通知订阅者
Notify(entry) {
// 检查是否有订阅者 返回队列
const cbQueue = this._events[entry.Event];
if (cbQueue && cbQueue.length) {
for (let callback of cbQueue) {
@ -55,51 +228,49 @@ class Socket {
}
}
// 请求数据的方法
// ✅ 修复 1:开启心跳保活机制,防止半小时后被服务器断开
onOpen(evt) {
// 每隔20s检查连接
// this.heartBeatTimer = setInterval(() => this.send({
// 'cmd': 'ping',
// 'args': ''
// }), 20000);
// 每次连接成功,先清除旧的心跳
if (this.heartBeatTimer) clearInterval(this.heartBeatTimer);
// 每隔20s发送一次心跳检查连接
this.heartBeatTimer = setInterval(() => {
// 确保是连接状态才发送
if (this.socket && this.socket.readyState === 1) {
this.send({
'cmd': 'ping',
'args': ''
});
}
}, 20000);
// 通知订阅
this.Notify({Event: 'open', Data : evt});
}
/**
* 订阅所有的数据
* @param {array|object} datas 订阅参数集合
* 发送数据
*/
send(datas) {
if (datas.constructor != Array) {
datas = [datas];
}
for (let item of datas) {
if (this.socket && this.socket.readyState === 1) {
this.socket.send(JSON.stringify(item));
}
}
}
onMessage(evt) {
try {
// 解析推送的数据
const data = JSON.parse(evt.data);
// 通知订阅者
this.Notify({
Event: 'message',
Data: data
});
} catch (err) {
console.error(' >> Data parsing error:', err);
// 通知订阅者
this.Notify({
Event: 'error',
Data: err
@ -120,53 +291,80 @@ class Socket {
// 订阅事件的方法
subscribe(name, handler) {
if (this._events[name]) {
this._events[name].push(handler); // 追加事件
this._events[name].push(handler);
} else {
this._events[name] = [handler]; // 添加事件
this._events[name] = [handler];
}
}
// 取消订阅事件
unsubscribe(name, handler) {
if (!this._events[name]) return;
let start = this._events[name].findIndex(item => item === handler);
// 删除该事件
if (start > -1) {
this._events[name].splice(start, 1);
}
}
checkOpen() {
return this.readyState >= 2;
}
// ✅ 修复 2:增加断线自动重连机制
reconnect() {
if (this.isReconnecting) return;
this.isReconnecting = true;
// 清除心跳
if (this.heartBeatTimer) clearInterval(this.heartBeatTimer);
console.log("WebSocket 掉线,正在尝试重新连接...");
// 延迟3秒重连,防止死循环无限重连导致卡死
this.reconnectTimer = setTimeout(() => {
this.socket = new WebSocket(this.link);
this.doOpen(); // 重新绑定事件
this.isReconnecting = false;
}, 3000);
}
onClose(evt) {
this.Notify({Event: 'close', Data : evt});
this.reconnect(); // 触发关闭时,自动去重连!
}
onError(err) {
this.Notify({Event: 'error', Data : err});
this.reconnect(); // 报错断开时,自动去重连!
}
// ✅ 修复 3:修复内存泄漏,获取一次数据后立刻销毁监听器
emit(data) {
return new Promise((resolve) => {
this.send(JSON.stringify(data));
this.on('message', function (data) {
resolve(data);
});
// const handler = (resData) => {
// resolve(resData);
// this.off('message', handler); // 收到消息后必须立刻解绑!
// };
this.on('message', handler);
});
}
doClose() {
if (this.socket) {
this.socket.close();
}
}
destroy() {
if (this.heartBeatTimer) {
clearInterval(this.heartBeatTimer);
if (this.heartBeatTimer) clearInterval(this.heartBeatTimer);
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
this.heartBeatTimer = null;
}
this.reconnectTimer = null;
this.doClose();
this._events = {};
this.readyState = 0;
@ -174,4 +372,4 @@ class Socket {
}
}
export default Socket
export default Socket;

31
static/chart_main/sevencoin.js

@ -73,7 +73,7 @@ $(function () {
this.self.subscribeBars(...arguments)
}
unsubscribeBars() {
this.self.unSub();
}
defaultSymbol() {
return {
@ -348,17 +348,44 @@ $(function () {
}
// 连接socket
linkSocket() {
// 连接socket
// this.Ws = new Ws(this.getQuery('ws'))
// this.Ws.on('message', (evt) => {
// if (evt.cmd == 'ping') {
// this.Ws.send({ cmd: 'pong' })
// }
// if (evt.type == 'ping') {
// this.Ws.send({ cmd: 'pong' })
// }
// // 追加数据
// // console.log(evt,evt.sub ,this.msg)
// if (evt.sub == this.msg) {
// this.onRealtimeCallback(this.getMap(evt.data))
// }
// })
// 连接socket
this.Ws = new Ws(this.getQuery('ws'))
// 新增:监听连接/重连成功事件
this.Ws.on('open', () => {
// 如果当前有需要订阅的币种信息,重连成功后立刻重新发送订阅指令给服务器!
if (this.msg) {
console.log('K线 WebSocket 已连接/重连,重新发送订阅...');
this.sub();
}
});
this.Ws.on('message', (evt) => {
// 响应服务端的 ping
if (evt.cmd == 'ping') {
this.Ws.send({ cmd: 'pong' })
}
if (evt.type == 'ping') {
this.Ws.send({ cmd: 'pong' })
}
// 追加数据
// console.log(evt,evt.sub ,this.msg)
if (evt.sub == this.msg) {
this.onRealtimeCallback(this.getMap(evt.data))
}

326
static/chart_main/ws.js

@ -1,6 +1,238 @@
"use strict";
// "use strict";
// function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
// function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
// function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
// function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
// function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
// function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// var Ws = /*#__PURE__*/function () {
// function Ws(ws, data) {
// var _this = this;
// _classCallCheck(this, Ws);
// // [{url, data, method...},,,,]
// this._ws = ws;
// this._data = data; // 待发送的消息列
// this._msgs = [];
// this.socket = this.doLink();
// this.doOpen(); // 订阅/发布模型
// this._events = {}; // 是否保持连接
// this._isLink = true; // 循环检查
// setInterval(function () {
// if (_this._isLink) {
// if (_this.socket.readyState == 2 || _this.socket.readyState == 3) {
// _this.resetLink();
// }
// }
// }, 3000);
// } // 重连
// _createClass(Ws, [{
// key: "resetLink",
// value: function resetLink() {
// this.socket = this.doLink();
// this.doOpen();
// } // 连接
// }, {
// key: "doLink",
// value: function doLink() {
// var ws = new WebSocket( this._ws);
// return ws;
// }
// }, {
// key: "doOpen",
// value: function doOpen() {
// var _this2 = this;
// this.socket.addEventListener('open',function (ev) {
// _this2.onOpen(ev);
// });
// this.socket.addEventListener('message',function (ev) {
// _this2.onMessage(ev);
// });
// this.socket.addEventListener('close',function (ev) {
// _this2.onClose(ev);
// });
// this.socket.addEventListener('error',function (ev) {
// _this2.onError(ev);
// });
// } // 打开
// }, {
// key: "onOpen",
// value: function onOpen() {
// var _this3 = this;
// // 打开时重发未发出的消息
// var list = Object.assign([], this._msgs);
// list.forEach(function (item) {
// if (_this3.send(item)) {
// var idx = _this3._msgs.indexOf(item);
// if (idx != -1) {
// _this3._msgs.splice(idx, 1);
// }
// }
// });
// } // 手动关闭
// }, {
// key: "doClose",
// value: function doClose() {
// this._isLink = false;
// this._events = {};
// this._msgs = [];
// this.socket.close({
// success: function success() {
// console.log('socket close success');
// }
// });
// } // 添加监听
// }, {
// key: "on",
// value: function on(name, handler) {
// this.subscribe(name, handler);
// } // 取消监听
// }, {
// key: "off",
// value: function off(name, handler) {
// this.unsubscribe(name, handler);
// } // 关闭事件
// }, {
// key: "onClose",
// value: function onClose() {
// var _this4 = this;
// // 是否重新连接
// if (this._isLink) {
// setTimeout(function () {
// _this4.resetLink();
// }, 3000);
// }
// } // 错误
// }, {
// key: "onError",
// value: function onError(evt) {
// this.Notify({
// Event: 'error',
// Data: evt
// });
// } // 接受数据
// }, {
// key: "onMessage",
// value: function onMessage(evt) {
// try {
// // 解析推送的数据
// var data = JSON.parse(evt.data); // 通知订阅者
// this.Notify({
// Event: 'message',
// Data: data
// });
// } catch (err) {
// console.error(' >> Data parsing error:', err); // 通知订阅者
// this.Notify({
// Event: 'error',
// Data: err
// });
// }
// } // 订阅事件的方法
// }, {
// key: "subscribe",
// value: function subscribe(name, handler) {
// if (this._events.hasOwnProperty(name)) {
// this._events[name].push(handler); // 追加事件
// } else {
// this._events[name] = [handler]; // 添加事件
// }
// } // 取消订阅事件
// }, {
// key: "unsubscribe",
// value: function unsubscribe(name, handler) {
// var start = this._events[name].findIndex(function (item) {
// return item === handler;
// }); // 删除该事件
// this._events[name].splice(start, 1);
// } // 发布后通知订阅者
// }, {
// key: "Notify",
// value: function Notify(entry) {
// // 检查是否有订阅者 返回队列
// var cbQueue = this._events[entry.Event];
// if (cbQueue && cbQueue.length) {
// var _iterator = _createForOfIteratorHelper(cbQueue),
// _step;
// try {
// for (_iterator.s(); !(_step = _iterator.n()).done;) {
// var callback = _step.value;
// if (_instanceof(callback, Function)) callback(entry.Data);
// }
// } catch (err) {
// _iterator.e(err);
// } finally {
// _iterator.f();
// }
// }
// } // 发送消息
// }, {
// key: "send",
// value: function send(data) {
// if (this.socket.readyState == 1) {
// this.socket.send(JSON.stringify(data));
// return true;
// } else {
// // 保存到待发送信息
// if (!this._msgs.includes(data)) {
// this._msgs.push(data);
// }
// ;
// return false;
// }
// }
// }]);
// return Ws;
// }();
// window.Ws = Ws;
"use strict";
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
@ -21,34 +253,43 @@ var Ws = /*#__PURE__*/function () {
_classCallCheck(this, Ws);
// [{url, data, method...},,,,]
this._ws = ws;
this._data = data; // 待发送的消息列
this._data = data;
this._msgs = [];
this.socket = this.doLink();
this.doOpen(); // 订阅/发布模型
this._events = {}; // 是否保持连接
// ✅ 新增:防重连风暴标识符与心跳定时器
this._isReconnecting = false;
this.pingTimer = null;
this._isLink = true; // 循环检查
this.socket = this.doLink();
this.doOpen();
this._events = {};
this._isLink = true;
// 循环检查连接状态
setInterval(function () {
if (_this._isLink) {
// ✅ 修复:如果已经在重连中了,就不要再触发了,防止灾难性复制
if (_this._isLink && !_this._isReconnecting) {
if (_this.socket.readyState == 2 || _this.socket.readyState == 3) {
_this.resetLink();
}
}
}, 3000);
} // 重连
}
_createClass(Ws, [{
key: "resetLink",
value: function resetLink() {
// ✅ 防止重复执行
if (this._isReconnecting) return;
this._isReconnecting = true;
if (this.pingTimer) clearInterval(this.pingTimer);
console.log('K线 WebSocket 正在重连...');
this.socket = this.doLink();
this.doOpen();
} // 连接
}
}, {
key: "doLink",
@ -73,13 +314,24 @@ var Ws = /*#__PURE__*/function () {
this.socket.addEventListener('error', function (ev) {
_this2.onError(ev);
});
} // 打开
}
}, {
key: "onOpen",
value: function onOpen() {
var _this3 = this;
// ✅ 连接成功,重置重连状态
this._isReconnecting = false;
// ✅ 新增:开启心跳保活机制
if (this.pingTimer) clearInterval(this.pingTimer);
this.pingTimer = setInterval(function () {
if (_this3.socket.readyState === 1) {
_this3.socket.send(JSON.stringify({ cmd: "ping", args: "" }));
}
}, 20000); // 20秒发送一次心跳
// 打开时重发未发出的消息
var list = Object.assign([], this._msgs);
list.forEach(function (item) {
@ -91,7 +343,7 @@ var Ws = /*#__PURE__*/function () {
}
}
});
} // 手动关闭
}
}, {
key: "doClose",
@ -99,37 +351,40 @@ var Ws = /*#__PURE__*/function () {
this._isLink = false;
this._events = {};
this._msgs = [];
if (this.pingTimer) clearInterval(this.pingTimer); // 清理心跳
this.socket.close({
success: function success() {
console.log('socket close success');
}
});
} // 添加监听
}
}, {
key: "on",
value: function on(name, handler) {
this.subscribe(name, handler);
} // 取消监听
}
}, {
key: "off",
value: function off(name, handler) {
this.unsubscribe(name, handler);
} // 关闭事件
}
}, {
key: "onClose",
value: function onClose() {
var _this4 = this;
if (this.pingTimer) clearInterval(this.pingTimer);
// 是否重新连接
if (this._isLink) {
if (this._isLink && !this._isReconnecting) {
setTimeout(function () {
_this4.resetLink();
}, 3000);
}
} // 错误
}
}, {
key: "onError",
@ -138,55 +393,51 @@ var Ws = /*#__PURE__*/function () {
Event: 'error',
Data: evt
});
} // 接受数据
}
}, {
key: "onMessage",
value: function onMessage(evt) {
try {
// 解析推送的数据
var data = JSON.parse(evt.data); // 通知订阅者
var data = JSON.parse(evt.data);
this.Notify({
Event: 'message',
Data: data
});
} catch (err) {
console.error(' >> Data parsing error:', err); // 通知订阅者
console.error(' >> Data parsing error:', err);
this.Notify({
Event: 'error',
Data: err
});
}
} // 订阅事件的方法
}
}, {
key: "subscribe",
value: function subscribe(name, handler) {
if (this._events.hasOwnProperty(name)) {
this._events[name].push(handler); // 追加事件
this._events[name].push(handler);
} else {
this._events[name] = [handler]; // 添加事件
this._events[name] = [handler];
}
}
} // 取消订阅事件
}, {
key: "unsubscribe",
value: function unsubscribe(name, handler) {
if (!this._events[name]) return;
var start = this._events[name].findIndex(function (item) {
return item === handler;
}); // 删除该事件
});
if(start > -1) {
this._events[name].splice(start, 1);
} // 发布后通知订阅者
}
}
}, {
key: "Notify",
value: function Notify(entry) {
// 检查是否有订阅者 返回队列
var cbQueue = this._events[entry.Event];
if (cbQueue && cbQueue.length) {
@ -204,7 +455,7 @@ var Ws = /*#__PURE__*/function () {
_iterator.f();
}
}
} // 发送消息
}
}, {
key: "send",
@ -213,12 +464,9 @@ var Ws = /*#__PURE__*/function () {
this.socket.send(JSON.stringify(data));
return true;
} else {
// 保存到待发送信息
if (!this._msgs.includes(data)) {
this._msgs.push(data);
}
;
return false;
}
}

Loading…
Cancel
Save