Created
March 20, 2012 00:37
-
-
Save tiagodavi/2129091 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<!-- jquery CDN --> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> | |
</script> | |
<script type="text/javascript"> | |
$(function(){ | |
//Verifica se temos acesso ao recurso | |
if(window.EventSource){ | |
//Criamos uma nova "escuta" com mensagens.php | |
var event_source = new EventSource('mensagens.php'); | |
//Capturamentos tudo que mensagens.php tem a dizer... :) | |
event_source.onmessage = function(e){ | |
//Convertemos o dados para Json | |
var data = JSON.parse(e.data); | |
//Enviamos para a div #mensagens com jquery | |
$('#mensagens').append('ID:'+ data.id + ' = ' + data.msg + '<br>'); | |
} | |
}else{ | |
//Utilize Chrome ou Firefox! | |
console.log("Você deve estar utilizando Internet Explorer =D"); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="mensagens"> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment