2018-08-23 21:40:27 +08:00

43 lines
1.1 KiB
JavaScript

import { dispatch } from '../third_party';
/**
* Socket工具类
* @author tengge / https://github.com/tengge1
* @param {*} url Socket服务器地址
*/
function Socket(url) {
this.url = url;
this.reconnectTime = 5000; // 重新连接时间
this.socket = new WebSocket(this.url);
this.dispatch = dispatch('open', 'message', 'error', 'close');
var _this = this;
this.socket.onopen = function (evt) {
_this.dispatch.call.apply(_this.dispatch, arguments);
};
this.socket.onmessage = function (evt) {
_this.dispatch.call.apply(_this.dispatch, arguments);
};
this.socket.onerror = function (evt) {
_this.dispatch.call.apply(_this.dispatch, arguments);
};
this.socket.onclose = function (evt) {
_this.dispatch.call.apply(_this.dispatch, arguments);
if (this.reconnectTime != null) {
setTimeout(function () {
_this.socket = new WebSocket(this.url);
}, this.reconnectTime);
}
};
}
Socket.prototype.on = function (eventName, callback) {
this.dispatch.on(eventName, callback);
};
export default Socket;