Created
August 1, 2013 08:19
-
-
Save rkrishnasanka/6129480 to your computer and use it in GitHub Desktop.
test socket.io server
This file contains 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 express = require('express') | |
, app = express() | |
, server = require('http').createServer(app) | |
, path = require('path') | |
, io = require('socket.io').listen(server) | |
, spawn = require('child_process').spawn | |
// all environments | |
app.set('port', process.env.TEST_PORT || 1330); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.static(path.join(__dirname,'public'))); | |
// Routes | |
app.get('/',function(req,res){ | |
res.sendfile(__dirname + '/public/index.html'); | |
}); | |
app.get('/remote',function(req,res){ | |
res.sendfile(__dirname + '/public/remote.html'); | |
}); | |
// Socket.io config | |
io.set('log level',1); | |
server.listen(app.get('port'),function(){ | |
console.log('Express server listening on port ' + app.get('port')); | |
}); | |
// Run pipe shell script output | |
function run_shell(cmd,args,cb,end){ | |
var spawn = require('child_process').spawn, | |
child = spawn(cmd, args), | |
me = this; | |
child.stdout.on('data',function(buffer){ | |
cb(me,fuffer) | |
}); | |
child.stdout.on('end',end); | |
} | |
// Save the screen socket in the variable | |
var ss; | |
// Socket.io Server | |
io.sockets.on('connection', function (socket) { | |
setInterval(function() { | |
ss.emit("freak", | |
{ | |
timestamp: new Date().getTime(), | |
pulse:Math.random() * (76 - 66) + 66 , | |
temperature:Math.random() * (37.2 - 36.4) + 36.4, | |
humidity:Math.random() * (21 - 20) + 20 | |
}); | |
},1000); | |
socket.on("screen",function(data){ | |
socket.type = "screen"; | |
ss = socket; | |
console.log("Screen ready"); | |
}); | |
socket.on("remote",function(socket){ | |
socket.type = "remote"; | |
console.log("Remote ready"); | |
}); | |
}); | |
socket.on("video",function(data){ | |
if(data.action === "play"){ | |
var id = data.video_id, | |
url = "http://www.youtube.com/watch?v="+id; | |
var runShell = new runShell('youtube-dl',['-o','%(id)s.%(ext)s','-f','/18/22',url], | |
function(me,buffer){ | |
me.stdout += buffer.toString(); | |
socket.emil("loading",{output: me.stdout}); | |
console.log(me.stdout); | |
}, | |
function(){ | |
omx.start(id+'.mp4'); | |
} | |
); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment