Skip to content

Instantly share code, notes, and snippets.

@lad1337
Created September 15, 2011 11:18
Show Gist options
  • Save lad1337/1219028 to your computer and use it in GitHub Desktop.
Save lad1337/1219028 to your computer and use it in GitHub Desktop.
a websocket server for NodeJS that creates growl notifications with the help of growlnotify
var port = 8000;
var WebSocketServer = require('websocket').server;
var http = require('http');
var exec = require('child_process').exec
var child;
function createGrowlNotification(cmd){
child = exec('growlnotify -m "'+cmd.body+'" -t "'+cmd.title+'" --image "'+cmd.icon+'"',
function (error, stdout, stderr) {
});
}
// the main server
var server = http.createServer(function(request, response) {
console.log((new Date()) + " Received request for " + request.url);
response.writeHead(404);
response.end();
});
server.listen(port, function() {
console.log((new Date()) + " Server is listening on port "+port);
});
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: true
});
wsServer.on('connect', function(connection) {
console.log((new Date()) + " Connection accepted.");
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received Message: " + message.utf8Data);
createGrowlNotification(JSON.parse(message.utf8Data))
connection.sendUTF("growlnotify cmd executed");
}
else if (message.type === 'binary') {
console.log("Received Binary Message of " + message.binaryData.length + " bytes");
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(connection) {
console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment