1 |
|
2 | (function() {
|
3 | var EventEmitter, HandshakeTimeout, LRWebSocketConnection, Parser, debug,
|
4 | __hasProp = {}.hasOwnProperty,
|
5 | __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; };
|
6 |
|
7 | debug = require('debug')('livereload:server');
|
8 |
|
9 | EventEmitter = require('events').EventEmitter;
|
10 |
|
11 | Parser = require('livereload-protocol');
|
12 |
|
13 | HandshakeTimeout = 1000;
|
14 |
|
15 | LRWebSocketConnection = (function(_super) {
|
16 |
|
17 | __extends(LRWebSocketConnection, _super);
|
18 |
|
19 | function LRWebSocketConnection(socket, id, options) {
|
20 | var protocols,
|
21 | _this = this;
|
22 | this.socket = socket;
|
23 | this.id = id;
|
24 | this.options = options;
|
25 | protocols = {
|
26 | monitoring: [Parser.protocols.MONITORING_7],
|
27 | conncheck: [Parser.protocols.CONN_CHECK_1]
|
28 | };
|
29 | if (this.options.protocols.saving >= 1) {
|
30 | protocols.saving = [Parser.protocols.SAVING_1];
|
31 | }
|
32 | this.parser = new Parser('server', protocols);
|
33 | this.socket.on('message', function(data) {
|
34 | debug("LRWebSocketConnection(" + _this.id + ") received " + data);
|
35 | return _this.parser.received(data);
|
36 | });
|
37 | this.socket.on('close', function() {
|
38 | if (_this._handshakeTimeout) {
|
39 | clearTimeout(_this._handshakeTimeout);
|
40 | _this._handshakeTimeout = null;
|
41 | }
|
42 | return _this.emit('disconnected');
|
43 | });
|
44 | this.parser.on('error', function(err) {
|
45 | _this.socket.close();
|
46 | return _this.emit('error', err);
|
47 | });
|
48 | this.parser.on('command', function(command) {
|
49 | if (command.command === 'ping') {
|
50 | return _this.send({
|
51 | command: 'pong',
|
52 | token: command.token
|
53 | });
|
54 | } else {
|
55 | return _this.emit('command', command);
|
56 | }
|
57 | });
|
58 | this.parser.on('connected', function() {
|
59 | if (_this._handshakeTimeout) {
|
60 | clearTimeout(_this._handshakeTimeout);
|
61 | _this._handshakeTimeout = null;
|
62 | }
|
63 | _this.send(_this.parser.hello(_this.options));
|
64 | return _this.emit('connected');
|
65 | });
|
66 | this._handshakeTimeout = setTimeout((function() {
|
67 | _this._handshakeTimeout = null;
|
68 | return _this.socket.close();
|
69 | }), HandshakeTimeout);
|
70 | }
|
71 |
|
72 | LRWebSocketConnection.prototype.close = function() {
|
73 | return this.socket.close();
|
74 | };
|
75 |
|
76 | LRWebSocketConnection.prototype.send = function(command) {
|
77 | this.parser.sending(command);
|
78 | return this.socket.send(JSON.stringify(command));
|
79 | };
|
80 |
|
81 | LRWebSocketConnection.prototype.isMonitoring = function() {
|
82 | var _ref;
|
83 | return ((_ref = this.parser.negotiatedProtocols) != null ? _ref.monitoring : void 0) >= 7;
|
84 | };
|
85 |
|
86 | return LRWebSocketConnection;
|
87 |
|
88 | })(EventEmitter);
|
89 |
|
90 | module.exports = LRWebSocketConnection;
|
91 |
|
92 | }).call(this);
|