Skip to content

Instantly share code, notes, and snippets.

@matiasfha
Created June 10, 2011 15:14
Show Gist options
  • Save matiasfha/1019027 to your computer and use it in GitHub Desktop.
Save matiasfha/1019027 to your computer and use it in GitHub Desktop.
node.js + socket.io + thrift
<img src="/videoStream" id="video"/>
<script src="socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8080});
socket.connect();
socket.on('connect',function(){
document.getElementById('mensajes').innerHTML = "conectado";
})
setInterval(function(){
var image = document.getElementById('video');
image.src = '/videoStream?'+new Date().getTime();
},1000);
</script>
var EventEmitter = require('events').EventEmitter,
events = new EventEmitter(),
boundary = "videostream12345";
var sended = false;
/********************** HTTP SERVER ********************************/
var express = require('express'),
fs = require('fs'),
app = express.createServer();
app.get('/',function(req,response){
fs.readFile(__dirname + '/index.html',function(err,data){
if(err) return send404(response);
response.writeHead(200,{'Content-Type':'text/html'});
response.write(data,'utf8');
response.end();
});
});
app.get('/videoStream',function(req,response){
response.writeHead(200, {
'Content-Type': 'multipart/x-mixed-replace;boundary="' + boundary + '"',
'Connection': 'keep-alive',
'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT',
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma': 'no-cache'
});
response.write("--" + boundary + "\n");
events.addListener('imagen_recibida',function(imagen){
response.write("Content-Type: image/jpeg\n Content-Length: " +imagen.data.length + "\n\n");
response.write(imagen.data);
response.write("\n--" + boundary + "\n");
});
});
app.listen(8080);
/*****************************************************************************/
/*********************** SOCKET.IO CONFIGURATION *****************************/
var io = require('socket.io'),
socket = io.listen(app);
/********************* THRIFT SERVER CONFIGURATION ***************************/
var thrift = require('thrift'),
Buffer = require('buffer').Buffer,
ImageService = require('./ImageService.js'),
ttypes = require('./service_types.js');
//This is call when thrift client send data
var receiveImage = function(image,success){
events.emit('imagen_recibida',image);
success(true);
}
var Tserver = thrift.createServer(ImageService,{
receiveImage: receiveImage
});
Tserver.listen(9090);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment