forked from redhog/agpsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
59 lines (47 loc) · 1.35 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var events = require('events');
var util = require('util');
var underscore = require('underscore');
var dateformat = require("dateformat");
exports.serverSockets = [];
exports.Router = function() {
var self = this;
self.on("directionChange", function (isClient) {
if (!isClient) {
self.emit("registerServer");
} else {
self.emit("unregisterServer");
}
});
self.stream.on("end", function () {
self.emit("unregisterServer");
});
self.stream.on("error", function (err) {
self.emit("unregisterServer");
});
self.on("registerServer", function (isClient) {
exports.serverSockets[self.name] = self;
});
self.on("unregisterServer", function (isClient) {
if (exports.serverSockets[self.name]) {
delete exports.serverSockets[self.name];
}
});
self.on('receiveCommand_WATCH', function (params) {
self.watch = params.enable;
});
self.on('receiveCommand_REPLAY', function (params) {
self.watch = true;
});
self.on('receiveResponse', function (response) {
if (!response.time) {
response.time = dateformat((new Date()), "isoDateTime");
}
for (var name in exports.serverSockets) {
var serverSocket = exports.serverSockets[name];
if (serverSocket.watch) {
serverSocket.sendResponse(response);
}
}
});
self.emit("directionChange", self.isClient);
}