/**
* 使用方法
* import WebSocket from '@/utils/websocket';
* const options = {
* url: 'ws://127.0.0.1:8080',
* onmessage: (res) => {
* // 接收数据后回调
* },
* // 保活周期 10s
* timer: 10000,
* // 断线重连
* reconnect: true,
* };
* const socket = new WebSocket(options);
*
* 手动关闭
* socket.close();
*/
class Socket {
/**
* 构造函数
* @param {object} params 构造函数参数
*/
constructor(params) {
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) { // 检测浏览器支持
console.error('错误: 浏览器不支持websocket');
return;
}
this.websocket = null;
this.params = params;
this.socketInit(params);
}
/**
* 初始化socket
* @param {string} url WebSocket服务器将响应的URL
* @param {function} onopen open事件的回调函数
* @param {function} onclose close事件的回调函数
* @param {function} onerror error事件的回调函数
* @param {function} onclose close事件的回调函数
* @param {boolean} reconnect 是否启用断线重连 默认为false
* @param {number} timer 心跳周期 若为空或0则不启用
*/
socketInit({
url,
onopen,
onmessage,
onerror,
onclose,
reconnect = false,
timer,
}) {
if (url !== undefined) {
// 若不以wss?开头
if (!/^wss?:\/\//.test(url)) {
const {
protocol
} = window.location;
// 则自动补上协议头
url = protocol === 'http:' ? 'ws://' + url : 'wss://' + url;
}
try {
this.websocket = new WebSocket(url);
// 用于指定连接成功后的回调函数。
this.websocket.onopen = (e) => {
if (timer > 0) {
this.heartCheck(timer);
}
if (typeof onopen === 'function') {
onopen(e);
}
};
// 用于指定当从服务器接受到信息时的回调函数。
this.websocket.onmessage = (e) => {
onmessage(e);
};
// 用于指定连接关闭后的回调函数。
this.websocket.onclose = (e) => {
reconnect && this.reconnect();
if (typeof onclose === 'function') {
onclose(e);
}
};
// 用于指定连接失败后的回调函数。
this.websocket.onerror = (e) => {
console.log('连接异常', e);
reconnect && this.reconnect();
if (typeof onerror === 'function') {
onerror(e);
}
};
} catch (e) {
reconnect && this.reconnect();
}
}
}
// 发送消息
send(message) {
// readyState 1 表示已经链接并且可以通讯
if (!this.websocket || this.websocket.readyState !== 1) {
console.log('请确认websocket是否已经链接并且可以通讯');
return;
}
this.websocket.send(JSON.stringify(message));
}
// 手动关闭socket
close() {
this.heartInterval && clearInterval(this.heartInterval);
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
if (!this.websocket) {
console.log('websocket 不可用');
return;
}
this.websocket.close();
}
// 周期性发送ping 保活
heartCheck(timer) {
this.heartInterval = window.setInterval(() => {
this.send({
type: 'ping'
});
}, timer);
}
// 断线重连
reconnect() {
if (this.lockReconnect) return;
this.lockReconnect = true;
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = window.setTimeout(() => {
this.socketInit(this.params);
this.lockReconnect = false;
}, 5000);
}
}
export default Socket;
WebSocket 封装
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.新建webSocket.js class webSocketClass { constructor(url,...
- 1,封装 websocket,创建websocket.js ,websocket 代码如下 2,首先在使用 web...