Skip to content

Instantly share code, notes, and snippets.

@brentjanderson
Last active December 14, 2015 01:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentjanderson/5010182 to your computer and use it in GitHub Desktop.
Save brentjanderson/5010182 to your computer and use it in GitHub Desktop.
Cocoaheads NeuNode demo. Get the repo: https://github.com/snakajima/neunode
// Paste this into one of the *.js files in the NeuNode project
var net = require('net');
var client = net.connect({port: 1337, host:'10.24.11.168'}, function() { //'connect' listener
console.log("Client connected");
});
client.on('data', function(data) {
console.log(data.toString());
});
client.on('end', function() {
console.log('client disconnected');
});
// Paste into a file on your desktop, run `node server.js` to fire it up
var news = [
"Borussia Dortmund wins German championship",
"Tornado warning for the Bay Area",
"More rain for the weekend",
"Android tablets take over the world",
"iPad2 sold out",
"Nation's rappers down to last two samples"
];
var net = require('net');
var clients = [];
var server = net.createServer(function (socket) {
clients.push(socket);
console.log("client connected");
socket.on('end', function() {
console.log("client disconnected");
clients.splice(clients.indexOf(socket),1);
});
});
setInterval(broadcastNew, 3000);
function broadcastNew() {
var message = new Buffer(news[Math.floor(Math.random()*news.length)]);
for (var s in clients) {
var socket = clients[s];
socket.write(message);
}
console.log(message + " over the wire...");
}
server.listen(1337, function() { //'listening' listener
console.log('Server bound on port 1337');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment