ソースを参照

load the history on initial load.

inefficiently as well, but we've got no api yet, so it's probably ok.
maybe per-date would be a nice compromise? but that would potentially
mean lots of requests for going back through time, so an api would be
nicer.

it could even work with daily files, reading them into memory (potential
oops) and then serving ranges of dates. hm...
Lucas Stadler 11 年 前
コミット
5f08f2015f
共有1 個のファイルを変更した22 個の追加2 個の削除を含む
  1. 22 2
      js/babl/index.html

+ 22 - 2
js/babl/index.html

@ -14,6 +14,8 @@
14 14
			var messagesEl = document.getElementById("messages");
15 15
			var inputEl = document.getElementById("input");
16 16
17
			setTimeout(loadHistory, 0);
18
17 19
			var ws = new WebSocket("ws://localhost:8080");
18 20
19 21
			inputEl.onkeyup = function(ev) {
@ -30,8 +32,7 @@
30 32
				messagesEl.appendChild(msgEl);
31 33
			}
32 34
33
			ws.onmessage = function(ev) {
34
				var msg = JSON.parse(ev.data);
35
			function handleMessage(msg) {
35 36
				switch (msg.type) {
36 37
					case "message":
37 38
						displayMessage(msg);
@ -41,6 +42,11 @@
41 42
				}
42 43
			}
43 44
45
			ws.onmessage = function(ev) {
46
				var msg = JSON.parse(ev.data);
47
				handleMessage(msg);
48
			}
49
44 50
			function sendMessage(msg) {
45 51
				var msg = {
46 52
					type: "message",
@ -49,6 +55,20 @@
49 55
				console.log("sending: ", msg);
50 56
				ws.send(JSON.stringify(msg));
51 57
			}
58
59
			function loadHistory() {
60
				var xhr = new XMLHttpRequest();
61
				xhr.open('GET', location.href + "/../msgs.json");
62
				xhr.onreadystatechange = function() {
63
					if (xhr.readyState == XMLHttpRequest.DONE) {
64
						var msgs = JSON.parse(xhr.responseText);
65
						msgs.forEach(function(msg) {
66
							handleMessage(msg);
67
						});
68
					}
69
				}
70
				xhr.send();
71
			}
52 72
		</script>
53 73
	</body>
54 74
</html>