-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientChat.js
72 lines (65 loc) · 2.5 KB
/
clientChat.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
(function ($) {
'use strict';
// var inputBox = document.getElementById('inputBox');
// var displayBox = document.getElementById('displayBox');
// inputBox.focus();
$('#inputBox').focus();
// $('#displayBox').css('border', 'solid 3px red');
// inputBox.addEventListener('keyup', function (event) {
// if ((event.keyCode == 13) && (inputBox.value.trim() != '')) {
//// var newChat = document.createElement('div');
//// newChat.textContent = inputBox.value;
//// newChat.setAttribute('class', 'c hatMsg');
//// displayBox.appendChild(newChat);
// if (displayBox.innerText == '') {
// displayBox.innerText = inputBox.value;
// }
// else {
// displayBox.innerText = displayBox.innerText + '\n' + inputBox.value;
// }
// sendChatToServer(inputBox.value);
// // clear chatbox
// inputBox.value = '';
// }
// });
$('#inputBox').keyup(function(e) {
debugger;
if ((e.keyCode == 13) && ($('#inputBox').val().trim() != '') ) {
sendChatToServer($('#inputBox').val());
// clear chatbox
$('#inputBox').val('');
}
});
function sendChatToServer(chatMsg) {
$.ajax({ type: 'post',
url: '/chat',
//contentType: 'application/json;charset=UTF-8',
// data: JSON.stringify({message: chatMsg})}
data: {message: chatMsg}}
).done(function () {
console.log("sent message: '" + chatMsg + "'");
});
// var request = new XMLHttpRequest();
// request.open('post', '/chat');
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
// request.send(JSON.stringify({message: chatMsg}));
// console.log("sent message: '" + chatMsg + "'");
}
// function updateChat() {
// var allMsgs = JSON.parse(this.responseText);
// console.log('allmsgs = ' + allMsgs);
// displayBox.innerText = allMsgs.join('\n');
// }
// setInterval(function () {
// var request = new XMLHttpRequest();
// request.open('get', '/chat');
// request.onload = updateChat;
// request.send(null);
// }, 1000);
setInterval(function() {
$.ajax({url: '/chat'}).done(function(response) {
console.log('allmsgs = ' + response);
$('#displayBox').html(response.join("<br/>"));});
}
,1000);
}(window.jQuery));