-
-
Save huggler/2308257 to your computer and use it in GitHub Desktop.
Teste de Stream de Dados Continuo
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
<html> | |
<head> | |
<title>Dashboard</title> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
<script> | |
$(document).ready(function(){ | |
socket = io.connect(); | |
socket.on('update_dados', function(json){ | |
$("#div_dados").html(json.dados); | |
}); | |
socket.on('close_dados', function(){ | |
$("#div_dados").html('Fim da Transmissao!'); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h2>DASHBOARD</h2> | |
<p id="div_dados"></p> | |
</body> | |
</html> |
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 http = require('http'), fs = require('fs'); | |
//Escrevendo várias vezes no response (fazer com o curl para ver melhor) | |
var server = http.createServer(function(request, response) | |
{ | |
switch(request.url) | |
{ | |
case '/': | |
fs.readFile(__dirname + '/index.html', function (err, data){ | |
if (err) { | |
response.writeHead(500, {"Content-Type":"text/html"}); | |
return response.end('Error loading index.html'); | |
} else { | |
response.writeHead(200, {"Content-Type":"text/html"}); | |
response.end(data); | |
} | |
}); | |
break; | |
case '/stream': | |
response.writeHead(200, {"Content-Type":"text/json"}); | |
setInterval(function(){ response.write((new Date()).toLocaleTimeString()); }, 1000); | |
/* | |
file = __dirname + '/arquivo.txt'; | |
fs.watch(file, function(curr, prev){ | |
fs.readFile(file, function (err, dados){ | |
response.write(dados); | |
}); | |
}); | |
*/ | |
break; | |
} | |
}).listen(8080); | |
var io = require('socket.io').listen(server); | |
// Requisição client para buscar o stream, e envia via socket | |
var options = { | |
host: 'localhost', | |
port: 8080, | |
path: '/stream', | |
method: 'GET' | |
}; | |
var reqStream = http.request(options, function(res) | |
{ | |
if(res.statusCode==200) | |
{ | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk){ | |
io.sockets.emit('update_dados', { dados: chunk }); | |
}); | |
res.on('end', function(){ | |
io.sockets.emit('close_dados'); | |
}); | |
} else { | |
io.sockets.emit('close_dados'); | |
} | |
}); | |
reqStream.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment