Skip to content

Commit

Permalink
Add a repository for sockets to server #1
Browse files Browse the repository at this point in the history
  • Loading branch information
flowersinthesand committed May 17, 2015
1 parent a4836da commit 3fe044b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/server.js
Expand Up @@ -14,6 +14,10 @@ var url = require("url");
module.exports = function() {
// A server object.
var self = new events.EventEmitter();
// A repository of sockets consisting of opened socket and closed socket
// only. A socket has id but it doesn't need to be public now so that array
// is used to hide it. TODO
self.sockets = [];
// Options to configure server and client.
var options = {
// A heartbeat interval in milliseconds.
Expand All @@ -32,9 +36,17 @@ module.exports = function() {
// A link between Cettia protocol and Cettia transport protocol. `transport` is
// expected to be passed from Cettia transport server.
self.handle = function(transport) {
// Builds a socket on top of a transport and fires `socket` event to
// server.
self.emit("socket", createSocket(transport, options));
// Builds a socket on top of a transport
var socket = createSocket(transport, options);
// TODO
self.sockets.push(socket);
socket.on("close", function() {
// It equals to `self.sockets.remove(socket)`.
self.sockets.splice(self.sockets.indexOf(socket), 1);
});
// Fires `socket` event to server. This is the beginning of the socket
// life cycle.
self.emit("socket", socket);
};
return self;
};
Expand Down

0 comments on commit 3fe044b

Please sign in to comment.