-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScript1.js
100 lines (64 loc) · 2.52 KB
/
JavaScript1.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var chatApp = {};
chatApp.messages = [];
var source = new EventSource('https://chatapp1000.firebaseio.com/messages/.json');
chatApp.eventPut = function (e) {}
chatApp.messagePut = function () {
var nameLocation = document.getElementById('name');
var messageLocation = document.getElementById('message');
var name = nameLocation.value;
var message = messageLocation.value;
messageLocation.value = '';
var data = {
name: name,
message: message,
time: (new Date()).toLocaleTimeString()
};
var request = new XMLHttpRequest();
request.open('POST', 'https://chatapp1000.firebaseio.com/messages/.json', true)
request.onload = function () {
};
request.onerror = function () {
};
request.send(JSON.stringify(data));
};
chatApp.loadMessages = function () {
var request = new XMLHttpRequest();
request.open('GET', 'https://chatapp1000.firebaseio.com/messages/.json', false);
request.onload = function () {
var data = {};
if (this.status >= 200 && this.status < 400) {
data = JSON.parse(this.response);
for (var name in data) {
chatApp.messages.push(data[name]);
};
};
};
request.onerror = function () {
}
request.send();
};
chatApp.eventPut = function (e) {
var data = JSON.parse(e.data);
var h;
if (data.data.hasOwnProperty('message')) {
h = document.getElementById('output').innerHTML;
h += data.data.time + ' - ' + data.data.name + ' - ' + data.data.message + '
';
g = document.getElementById('output1').innerHTML;
g += data.data.time + ' - ' + data.data.name + ' - ' + data.data.message + '<br/>';
}
else {
h = document.getElementById('output').innerHTML;
g = document.getElementById('output1').innerHTML;
for (var i in data.data) {
h += data.data[i].time + ' - ' + data.data[i].name + ' - ' + data.data[i].message + '
';
g += data.data[i].time + ' - ' + data.data[i].name + ' - ' + data.data[i].message + '<br/>';
};
};
document.getElementById('output').innerHTML = h;
document.getElementById('output1').innerHTML = g;
var textarea = document.getElementById('output');
textarea.scrollTop = textarea.scrollHeight;
var divarea = document.getElementById('output1');
divarea.scrollTop = divarea.scrollHeight;
}
source.addEventListener('put', chatApp.eventPut, false)