Created
April 18, 2014 20:44
-
-
Save brunokruse/11063568 to your computer and use it in GitHub Desktop.
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
var net = require('net'); | |
var http = require('http'); | |
var request = require('request'); | |
// feed url (NYC coords) | |
var feedUrl = 'http://clients.rtorres.me:3000/?lat=40.6700&lon=-73.9400'; | |
// feed url (SF coords) | |
//var feedUrl = 'http://clients.rtorres.me:3000/?lat=37.7833&lon=-122.4167'; | |
// our send variables | |
var temperature = ""; | |
var city = ''; | |
var condition = ''; | |
// our array of clients (oF applications) | |
var clients = []; | |
var server = net.createServer(function(socket) { | |
// Identify this client | |
socket.name = socket.remoteAddress + ":" + socket.remotePort | |
// Add new client to the list | |
clients.push(socket); | |
socket.write("Hello from server! " + socket.name + "\n"); | |
socket.pipe(socket); | |
// Handle incoming messages from clients. | |
socket.on('data', function (data) { | |
var msg = data.toString(); | |
console.log('Received data message: ' + msg); | |
// we split the incoming data from new line | |
var parts = msg.split('\n'); | |
// parse commands | |
switch (parts[0]) { | |
case 'go': // fire json | |
var fireTime = new Date().getTime() + 500; | |
var msg = JSON.stringify( | |
{ | |
goTime:fireTime, | |
city:city, | |
temp:String(temperature), | |
condition:condition | |
}); | |
broadcast(msg + '[/TCP]'); | |
console.log ('sending JSON'); | |
console.log(msg); | |
break; | |
case 'feed': // get feed | |
assembleJSON(); | |
break; | |
default: // oops | |
broadcast('sorry, does not compute: ' + parts[0] + '\n'); | |
break; | |
} | |
}); | |
// Remove the client from the list when it leaves | |
socket.on('end', function () { | |
clients.splice(clients.indexOf(socket), 1); | |
}); | |
}); | |
function assembleJSON() { | |
request(feedUrl, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
//console.log(body) // Print the google web page. | |
var jsonResponse = JSON.parse(body); | |
//console.log(body); | |
city = jsonResponse['city']; | |
temperature = Math.round(jsonResponse['weather']['temperature']); | |
condition = jsonResponse['weather']['condition']; | |
console.log('city: ' + city); | |
console.log('current temp: ' + temperature + ' ' + condition); | |
} | |
}); | |
} | |
function broadcast (msg) { | |
// if there are no clients don't write | |
if (clients == null) return; | |
clients.forEach(function (client) { | |
//if (client === sender) return; | |
client.write(msg + '\n'); | |
}); | |
} | |
server.listen(1337, '10.0.1.4'); | |
assembleJSON(); | |
console.log('Listening for clients on 1337'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment