exports.__esModule = true;

var _sockjsClient = require('sockjs-client');

var _sockjsClient2 = _interopRequireDefault(_sockjsClient);

var _stompjs = require('stompjs');

var _stompjs2 = _interopRequireDefault(_stompjs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var WebSocket = function () {
  // 构造函数
  function WebSocket() {
    _classCallCheck(this, WebSocket);

    this.host = sessionStorage.getItem('wshost') || '';
    this.tryTimes = 1; // 重连次数
    this.callback = null; // 回调函数
    this.client = null; // stomp对象
    this.reconTimeout = null; // 重连延时器
    this.debug = true; // 调试
    this.interval = 20; // 重连间隔时间
    this.vm = null;
    this.nextDate = 0;
    this.connects = 1;
    this.recon = false;
    this.sendTimeout = null;
    this.socket = null;
    this.subscription = null;
  }

  /** socket连接 */


  WebSocket.prototype.connect = function connect() {
    var _this = this;

    if (!this.client) {
      // 连接SockJS
      this.socket = new _sockjsClient2.default(this.host + this.url, { timeout: 60000 });
      this.client = _stompjs2.default.over(this.socket);
    }

    // 日志不打印
    if (!this.debug) {
      this.client.debug = function () {};
    }

    // 向服务器发起websocket连接
    this.client.connect({}, function () {
      _this.recon = true;
      // tryTimes定义重置
      _this.connects = 1;
      // 订阅消息
      _this.subscribe();
    }, function (error) {
      _this.unsubscribe();
      _this.client = null;
      var diffSecond = parseInt((new Date() - _this.nextDate) / 1000, 10);
      if (_this.connects > 5 && diffSecond < _this.interval) {
        _this.error && _this.error(error);
      } else {
        _this.reconTimeout = setTimeout(function () {
          _this.connect();
          _this.connects++;
        }, 5000);
      }
    });
  }; // 清除订阅


  WebSocket.prototype.unsubscribe = function unsubscribe() {
    if (this.subscription) {
      this.subscription.unsubscribe();
      this.subscription = null;
    }
  };
  /** 订阅服务端 */


  WebSocket.prototype.subscribe = function subscribe() {
    var _this2 = this;

    // 订阅服务端提供的某个topic
    this.subscription = this.client.subscribe(this.take, function (response) {
      if (response && (_this2.callback || _this2.success)) {
        var callback = _this2.callback || _this2.success;
        callback(JSON.parse(response.body));
      }
    });
  };

  /** 赋值、初始化socket */


  WebSocket.prototype.init = function init(option, vm) {
    for (var i in option) {
      this[i] = option[i];
    }
    this.vm = vm;
    // 初始化连接
    this.connect();
  };

  /** 发送消息 */


  WebSocket.prototype.send = function send(data) {
    var _this3 = this;

    if (this.recon) {
      clearTimeout(this.sendTimeout);
      this.client.send(this.take, {}, typeof data === 'string' ? data : JSON.stringify(data));
    } else {
      this.sendTimeout = setTimeout(function () {
        _this3.send(data);
      }, 1000);
    }
  };

  /** 销毁 */


  WebSocket.prototype.destroy = function destroy() {
    // 断开连接,清除定时器
    this.unsubscribe();
    if (this.client) {
      this.client.disconnect();
    };
    this.reconTimeout && clearTimeout(this.reconTimeout);
    this.sendTimeout && clearTimeout(this.sendTimeout);
  };

  return WebSocket;
}();

exports.default = WebSocket;