aboutsummaryrefslogtreecommitdiffstats
path: root/example_chat.html
blob: 0fe35dd2c9b8a622496848df673334672cdcbf1d (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
<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 = "";
		for(var x in data.msgs)
		{
			s += data.msgs[x] + "<BR>";
		}
		$("#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>