//#region ******************************创建Websocket******************************
/**
* 创建websocket
* @param {object} opts -初始化配置项
*/
var CreateWebsocket = function (opts) {
if (!(this instanceof CreateWebsocket)) return new CreateWebsocket(opts);
//设定CreateWebsocket对象属性项opts初始默认值
this.url = null;//连接地址
this.ws = null;//websocket对象
this.interval = 60000;//默认隔60s发一次心跳
this.onopen = null;//socket的onopen事件
this.onclose = null; //socket的onclose事件
this.onmessage = null;//socket的onmessage事件
this.onerror = null;//socket的onerror事件
this.heartBeatData = null;//心跳测试发送的数据(规定测试数据须将GroupId设定为0),{ GroupId: 0, Content: '', SenderId: '', RecipientId: '', Createdt: '' }
this.init(opts);
};
CreateWebsocket.prototype = {
/**
* 初始化
* @param {object} opts -初始化配置信息
*/
init: function (opts) {
var count = 0;//统计重连上所需次数
var totalTime = 0;//重连花费时间
var timeOutObj = null;//轮询发送心跳setTimeout对象
var timeOu
创建WebSocket通讯
最新推荐文章于 2025-03-23 15:33:12 发布
本文详细介绍了如何使用WebSocket技术创建实时通讯应用。从建立连接、发送与接收消息到处理断开连接,全面覆盖WebSocket的基本操作。通过实例代码,演示了在前后端中集成WebSocket的步骤,帮助开发者掌握WebSocket在实时更新场景中的应用。


1万+

被折叠的 条评论
为什么被折叠?



