Last active
December 14, 2015 01:59
-
-
Save brentjanderson/5010182 to your computer and use it in GitHub Desktop.
Cocoaheads NeuNode demo. Get the repo: https://github.com/snakajima/neunode
This file contains hidden or 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
// 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'); | |
}); |
This file contains hidden or 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
// 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