/
Server.js
48 lines (37 loc) · 1.12 KB
/
Server.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
"use strict";
var net = require ("net");
var Server = function (hub) {
this.hub = hub;
this.connections = [];
};
/**
* Server.prototype.listen():
* Opens the port and starts listening for connections.
**/
Server.prototype.listen = function () {
var self = this;
this.server = net.createServer ();
this.server.on ("connection", function(socket) { self.connection (socket); });
this.server.on ("listening", function() { self.listening (); });
//this.server.listen (this.config.data.Core.socket);
};
/**
* Server.prototype.close():
* Terminates all connections and closes the port, refusing further connections.
**/
Server.prototype.close = function () {
this.connections.forEach (function (socket) {
socket.end (); // TODO: Send proper notification of shutdown.
});
this.connections = [];
this.server.close ();
};
Server.prototype.listening = function() {
console.log ("Now listening at %s", this.config.data.Core.socket);
};
Server.prototype.connection = function(socket) {
// TODO: Maintain a persistent connection.
console.log ("Petal connected");
socket.end ("Hello.\nGoodbye.\n");
};
module.exports = Server;