Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a network module to replace the socket io module
- Loading branch information
1 parent
6687287
commit 9c90757
Showing
4 changed files
with
84 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name":"Layout" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Load the TCP Library | ||
var net = require('net'); | ||
|
||
var network = { | ||
clients : [], | ||
createServer : function (port) { | ||
net.createServer(function(socket){ | ||
// Identify this client | ||
socket.name = socket.remoteAddress + ":" + socket.remotePort | ||
|
||
// Put this new client in the list | ||
network.clients.push(socket); | ||
|
||
console.log("Connected to: " + socket.name); | ||
|
||
// Handle incoming messages from clients. | ||
// socket.on('data', function (data) { | ||
// broadcast(socket.name + "> " + data, socket); | ||
// }); | ||
|
||
// Remove the client from the list when it leaves | ||
socket.on('end', function () { | ||
clients.splice(clients.indexOf(socket), 1); | ||
console.log(socket.name + " disconnected"); | ||
}); | ||
} , port); | ||
}, | ||
sendData : function (data) { | ||
clients.forEach(function (client) { | ||
// Don't want to send it to sender | ||
if (client === sender) return; | ||
client.write(message); | ||
}); | ||
} | ||
}; | ||
|
||
// Keep track of the chat clients | ||
var clients = []; | ||
|
||
// Start a TCP Server | ||
net.createServer(function (socket) { | ||
|
||
// Send a message to all clients | ||
function broadcast(message, sender) { | ||
clients.forEach(function (client) { | ||
// Don't want to send it to sender | ||
if (client === sender) return; | ||
client.write(message); | ||
}); | ||
// Log it to the server output too | ||
process.stdout.write(message) | ||
} | ||
|
||
}).listen(3000); | ||
|
||
module.exports = network; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"dependencies": { | ||
"minimist": "^1.2.0", | ||
"node-watch": "^0.5.1", | ||
"rootpath": "^0.1.2", | ||
"socket.io": "^1.7.2" | ||
} | ||
} |