aboutsummaryrefslogtreecommitdiffstats
path: root/examples/example_chat.html
blob: 233e093a46709024eafcecd552c2ae2fb9817224 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<input id="msg" type="text">
<button id="send">Send</button>
<div id="logs">
</div>
<script>
$(document).ready(function(){
	$("#send").click(function(){
		var msg = $("#msg").val();
		console.log(msg);
		if (msg.length > 0)
			$.post("/send", msg);
		$("#msg").val("");
	});
	$("#msg").keyup(function(event){
		if(event.keyCode == 13){
			$("#send").click();
		}
	});
	var lastLog = 0;
	var updateLog;
	updateLog = function(data)
	{
		console.log("recv ");
		console.log(data);
		var lastLog = data.last*1;
		console.log("lastLog: " + lastLog);
		var s = "";
        function htmlEncode(s)
        {
            return s.replace(/&(?!\w+([;\s]|$))/g, "&amp;")
            .replace(/</g, "&lt;").replace(/>/g, "&gt;");
        }
		for(var x in data.msgs)
		{

			s = htmlEncode(data.msgs[x]) + "<BR>" + s;
		}
		$("#logs").html(s+$("#logs").html());
		var failFunction;
		failFunction = function(){
			$.getJSON("/logs/"+lastLog, updateLog).fail(failFunction);
		};
		$.getJSON("/logs/"+lastLog, updateLog).fail(failFunction);
	}
	$.getJSON("/logs", updateLog);
});
</script>
</body>
</html>