1 |
|
2 | (function() {
|
3 | var DefaultWebSocketPort, EventEmitter, LRWebSocketConnection, LRWebSocketServer, Url, debug, fs, http, wsio,
|
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 | wsio = require('websocket.io');
|
10 |
|
11 | http = require('http');
|
12 |
|
13 | Url = require('url');
|
14 |
|
15 | fs = require('fs');
|
16 |
|
17 | EventEmitter = require('events').EventEmitter;
|
18 |
|
19 | LRWebSocketConnection = require('./connection');
|
20 |
|
21 | DefaultWebSocketPort = parseInt(process.env['LRPortOverride'], 10) || 35729;
|
22 |
|
23 | LRWebSocketServer = (function(_super) {
|
24 |
|
25 | __extends(LRWebSocketServer, _super);
|
26 |
|
27 | function LRWebSocketServer(options) {
|
28 | var _base, _ref;
|
29 | this.options = options;
|
30 | if (!this.options.id) throw new Error("ERR_INVALID_ARG: id is required");
|
31 | if (!this.options.name) throw new Error("ERR_INVALID_ARG: name is required");
|
32 | if (!this.options.version) {
|
33 | throw new Error("ERR_INVALID_ARG: version is required");
|
34 | }
|
35 | this.port = this.options.port || DefaultWebSocketPort;
|
36 | this.connections = {};
|
37 | this.activeConnections = 0;
|
38 | this.nextConnectionId = 1;
|
39 | if ((_ref = (_base = this.options).protocols) == null) _base.protocols = {};
|
40 | }
|
41 |
|
42 | LRWebSocketServer.prototype.listen = function(callback) {
|
43 | var callbackCalled,
|
44 | _this = this;
|
45 | callbackCalled = false;
|
46 | this.httpServer = http.createServer();
|
47 | try {
|
48 | this.httpServer.on('error', function(err) {
|
49 | if (!callbackCalled) {
|
50 | callbackCalled = true;
|
51 | return callback(err);
|
52 | } else {
|
53 | throw err;
|
54 | }
|
55 | });
|
56 | return this.httpServer.listen(this.port, function(err) {
|
57 | if (err) {
|
58 | callbackCalled = true;
|
59 | return callback(err);
|
60 | }
|
61 | _this.httpServer.on('request', function(request, response) {
|
62 | return request.on('end', function() {
|
63 | var url;
|
64 | url = Url.parse(request.url, true);
|
65 | if (url.pathname === '/livereload.js' || url.pathname === '/xlivereload.js') {
|
66 | return _this.emit('livereload.js', request, response);
|
67 | } else {
|
68 | return _this.emit('httprequest', url, request, response);
|
69 | }
|
70 | });
|
71 | });
|
72 | _this.wsserver = wsio.attach(_this.httpServer);
|
73 | _this.wsserver.on('connection', function(socket) {
|
74 | return _this._createConnection(socket);
|
75 | });
|
76 | return callback(null);
|
77 | });
|
78 | } catch (e) {
|
79 | return callback(e);
|
80 | }
|
81 | };
|
82 |
|
83 | LRWebSocketServer.prototype.close = function() {
|
84 | var connection, _, _ref;
|
85 | this.httpServer.close();
|
86 | _ref = this.connections;
|
87 | for (_ in _ref) {
|
88 | if (!__hasProp.call(_ref, _)) continue;
|
89 | connection = _ref[_];
|
90 | connection.close();
|
91 | }
|
92 | };
|
93 |
|
94 | LRWebSocketServer.prototype.monitoringConnections = function() {
|
95 | var connection, dummy, _ref, _results;
|
96 | _ref = this.connections;
|
97 | _results = [];
|
98 | for (dummy in _ref) {
|
99 | if (!__hasProp.call(_ref, dummy)) continue;
|
100 | connection = _ref[dummy];
|
101 | if (connection.isMonitoring()) _results.push(connection);
|
102 | }
|
103 | return _results;
|
104 | };
|
105 |
|
106 | LRWebSocketServer.prototype.monitoringConnectionCount = function() {
|
107 | return this.monitoringConnections().length;
|
108 | };
|
109 |
|
110 | LRWebSocketServer.prototype._createConnection = function(socket) {
|
111 | var connection,
|
112 | _this = this;
|
113 | connection = new LRWebSocketConnection(socket, "C" + (this.nextConnectionId++), this.options);
|
114 | connection.on('connected', function() {
|
115 | _this.connections[connection.id] = connection;
|
116 | return _this.emit('connected', connection);
|
117 | });
|
118 | connection.on('disconnected', function() {
|
119 | delete _this.connections[connection.id];
|
120 | return _this.emit('disconnected', connection);
|
121 | });
|
122 | connection.on('command', function(command) {
|
123 | return _this.emit('command', connection, command);
|
124 | });
|
125 | connection.on('error', function(err) {
|
126 | return _this.emit('error', err, connection);
|
127 | });
|
128 | return connection;
|
129 | };
|
130 |
|
131 | return LRWebSocketServer;
|
132 |
|
133 | })(EventEmitter);
|
134 |
|
135 | module.exports = LRWebSocketServer;
|
136 |
|
137 | }).call(this);
|