// Generated by IcedCoffeeScript 1.8.0-d
(function() {
  var EventEmitter, HandshakeTimeout, LRWebSocketConnection, Parser, debug,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  debug = require('debug')('livereload:server');

  EventEmitter = require('events').EventEmitter;

  Parser = require('livereload-protocol');

  HandshakeTimeout = 1000;

  LRWebSocketConnection = (function(_super) {
    __extends(LRWebSocketConnection, _super);

    function LRWebSocketConnection(socket, id, options) {
      var protocols;
      this.socket = socket;
      this.id = id;
      this.options = options;
      protocols = {
        monitoring: [Parser.protocols.MONITORING_7],
        conncheck: [Parser.protocols.CONN_CHECK_1]
      };
      if (this.options.protocols.saving >= 1) {
        protocols.saving = [Parser.protocols.SAVING_1];
      }
      this.parser = new Parser('server', protocols);
      this.socket.on('message', (function(_this) {
        return function(data) {
          debug("LRWebSocketConnection(" + _this.id + ") received " + data);
          return _this.parser.received(data);
        };
      })(this));
      this.socket.on('close', (function(_this) {
        return function() {
          if (_this._handshakeTimeout) {
            clearTimeout(_this._handshakeTimeout);
            _this._handshakeTimeout = null;
          }
          return _this.emit('disconnected');
        };
      })(this));
      this.socket.on('error', (function(_this) {
        return function(err) {
          return console.error(err);
        };
      })(this));
      this.parser.on('error', (function(_this) {
        return function(err) {
          _this.socket.close();
          return _this.emit('error', err);
        };
      })(this));
      this.parser.on('command', (function(_this) {
        return function(command) {
          if (command.command === 'ping') {
            return _this.send({
              command: 'pong',
              token: command.token
            });
          } else {
            return _this.emit('command', command);
          }
        };
      })(this));
      this.parser.on('connected', (function(_this) {
        return function() {
          if (_this._handshakeTimeout) {
            clearTimeout(_this._handshakeTimeout);
            _this._handshakeTimeout = null;
          }
          _this.send(_this.parser.hello(_this.options));
          return _this.emit('connected');
        };
      })(this));
      this._handshakeTimeout = setTimeout(((function(_this) {
        return function() {
          _this._handshakeTimeout = null;
          return _this.socket.close();
        };
      })(this)), HandshakeTimeout);
    }

    LRWebSocketConnection.prototype.close = function() {
      return this.socket.close();
    };

    LRWebSocketConnection.prototype.send = function(command) {
      this.parser.sending(command);
      return this.socket.send(JSON.stringify(command));
    };

    LRWebSocketConnection.prototype.isMonitoring = function() {
      var _ref;
      return ((_ref = this.parser.negotiatedProtocols) != null ? _ref.monitoring : void 0) >= 7;
    };

    return LRWebSocketConnection;

  })(EventEmitter);

  module.exports = LRWebSocketConnection;

}).call(this);
