lad1337

NodeJS websocket sever that creates growl notifications

Sep 14th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var port = 8000;
  2.  
  3. var WebSocketServer = require('websocket').server;
  4. var http = require('http');
  5. var exec = require('child_process').exec
  6. var child;
  7.  
  8. function createGrowlNotification(cmd){
  9.    
  10.             child = exec('growlnotify -m "'+cmd.body+'" -t "'+cmd.title+'" --image "'+cmd.icon+'"',
  11.                 function (error, stdout, stderr) {
  12.             });
  13. }
  14.  
  15.  
  16. // the main server
  17. var server = http.createServer(function(request, response) {
  18.     console.log((new Date()) + " Received request for " + request.url);
  19.     response.writeHead(404);
  20.     response.end();
  21. });
  22. server.listen(port, function() {
  23.     console.log((new Date()) + " Server is listening on port "+port);
  24. });
  25.  
  26. wsServer = new WebSocketServer({
  27.     httpServer: server,
  28.     autoAcceptConnections: true
  29. });
  30.  
  31. wsServer.on('connect', function(connection) {
  32.     console.log((new Date()) + " Connection accepted.");
  33.     connection.on('message', function(message) {
  34.         if (message.type === 'utf8') {
  35.             console.log("Received Message: " + message.utf8Data);
  36.             createGrowlNotification(JSON.parse(message.utf8Data))
  37.             connection.sendUTF("growlnotify cmd executed");
  38.         }
  39.         else if (message.type === 'binary') {
  40.             console.log("Received Binary Message of " + message.binaryData.length + " bytes");
  41.             connection.sendBytes(message.binaryData);
  42.         }
  43.     });
  44.     connection.on('close', function(connection) {
  45.         console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
  46.     });
  47. });
Advertisement
Add Comment
Please, Sign In to add comment