Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes.

  • Loading branch information...
commit 7bfe127f4377187b3e4e3cf87f7539a10bd28ac4 1 parent 8cb3be2
@Xorcerer authored
Showing with 18 additions and 9 deletions.
  1. +18 −9 agent.js
View
27 agent.js
@@ -1,19 +1,23 @@
+var events = require('events');
+
var Agent = function () {
this.lastId = 0;
- // TODO: Is it effe
+ // TODO: Is it effetive way to manage mutable dictionary in V8.
this.clients = {};
+ var agent = this;
+
this.onConnected = function(client) {
var clientId = this.lastId += 1;
this.clients[clientId] = client;
+ client.id = clientId;
- console.log('client connected');
+ console.log('client', clientId, 'connected');
- var agent = this;
client.on('end', function() {
delete agent.clients[clientId];
- console.log('client disconnected');
+ console.log('client', clientId, 'disconnected');
});
// TODO: Package completeness check.
@@ -26,13 +30,18 @@ var Agent = function () {
return;
}
- Object.keys(agent.clients).forEach(function(id) {
- var c = agent.clients[id];
- if (c != client)
- c.write(JSON.stringify(position));
- });
+ agent.broadcast(position);
+ });
+ };
+
+ this.broadcast = function(message) {
+ Object.keys(this.clients).forEach(function(id) {
+ var c = agent.clients[id];
+ c.write(JSON.stringify(message));
});
};
};
+Agent.prototype.__proto__ = events.EventEmitter.prototype;
+
exports.Agent = Agent;
Please sign in to comment.
Something went wrong with that request. Please try again.