-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 1.44 KB
/
index.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
//URL = "http://192.168.25.19:8000"
URL = "http://192.168.25.107:8000"
function send(ev){
var msg = document.querySelector('#js-text-input')
var chatElement = document.querySelector('#js-chatlogs')
var newMsg = document.createElement('p')
newMsg.classList = ['chat-message']
newMsg.innerHTML = msg.value
var newDiv = document.createElement('div')
newDiv.classList = ['chat self']
newDiv.appendChild(newMsg)
chatElement.appendChild(newDiv)
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
//console.log('All right!')
}
}
xhr.open('POST', URL, true)
xhr.send(msg.value)
msg.value = ""
}
function receive(){
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
dic = JSON.parse(xhr.responseText)
messages = dic['result']
for (var idx in messages){
var servMsg = messages[idx]
var chatElement = document.querySelector('#js-chatlogs')
var newMsg = document.createElement('p')
newMsg.classList = ['chat-message']
newMsg.innerHTML = servMsg
var newDiv = document.createElement('div')
newDiv.classList = ['chat friend']
newDiv.appendChild(newMsg)
chatElement.appendChild(newDiv)
}
}
}
xhr.open('GET', URL, true)
xhr.send()
}
function main(){
var el = document.querySelector('#js-button')
//console.log('!!!',el)
el.onclick = send
setInterval(receive, 1000)
}
document.addEventListener('DOMContentLoaded', main)