Permalink
Browse files

Update the tcp module.

  • Loading branch information...
1 parent 1ec7bdc commit 6499401990f13aa33eb2c3fea88b6bd26afd72be @iwillwen committed Jan 16, 2012
Showing with 10 additions and 16 deletions.
  1. +10 −16 lib/net.js
View
@@ -1,40 +1,34 @@
-var net = require('net'),
- events = require('events'),
- sockets = [],
- util = require('util');
+var net = require('net');
+var sockets = [];
+var util = require('util');
-exports.net = function (port, callback) {
+exports.net = function (callback) {
var tcpServer = net.createServer(function (socket) {
- util.inherits(socket, events.EventEmitter);
socket.id = Math.round(Math.random() * 10000000000);
socket.on('connect', function () {
- for (var i = 0; i < socket.listeners('connection').length; i++) {
- socket.listeners('connection')[i]();
- }
+ socket.emit('connection');
});
socket.on('data', function (data) {
- for (var i = 0; i < socket.listeners('message').length; i++) {
- socket.listeners('message')[i](data);
- }
+ socket.emit('message', data);
});
socket.on('end', function () {
var a = sockets.indexOf(socket);
sockets.splice(a, 1);
- for (var i = 0; i < socket.listeners('disconnect').length; i++) {
- socket.listeners('disconnect')[i]();
- }
+ socket.emit('disconnect');
});
socket.send = function (str) {
socket.write(str);
+ return this;
};
socket.broadcast = function (data) {
for (var i = 0; i < sockets.length; i++) {
if (sockets[i] == socket) continue;
sockets[i].write(data);
}
+ return this;
};
sockets.push(socket);
callback(socket);
- }).listen(port);
+ });
return tcpServer;
};

0 comments on commit 6499401

Please sign in to comment.