forked from IvanoAlvino/ChatRoom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanageChat.js
More file actions
130 lines (102 loc) · 3.41 KB
/
manageChat.js
File metadata and controls
130 lines (102 loc) · 3.41 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// global variables
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastReceivedMessage = 0;
var timer;
// frequency is the refresh poll value expressed in ms
var frequency = 2000;
timer = setInterval("getUnreadMessages();", frequency);
// get the browser dependent XMLHttpRequest object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
else {
document.getElementById('status').innerHTML =
'Status: Error while creating XmlHttpRequest Object.';
}
}
// start operations for receiving unread messages from server
function getUnreadMessages() {
if (receiveReq) {
// set the listener now for the response
receiveReq.onreadystatechange = receiveMessages;
// fill param to send using POST
var param = "lastReceivedMessage=" + lastReceivedMessage;
// open and send request
receiveReq.open("POST", 'chatServer.php', true);
receiveReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
receiveReq.send(param);
}
}
// receive messages sent from server and print them in chat
function receiveMessages() {
// refresh chat in timer seconds
if ( receiveReq.readyState == 4 && receiveReq.status == 200) {
// get the chat div reference
var chat_div = document.getElementById("div_chat");
// eval the AJAX response
var response = eval(receiveReq.responseText);
// for every message
for(i=0; i < response.length; i++) {
// print sender name, time and message
chat_div.innerHTML += '<font class="username_font">' + response[i].sender + ' </font>';
chat_div.innerHTML += '<font class="time_font">' + response[i].time + ' UTC</font><br>';
chat_div.innerHTML += '<font class="message_font">' + response[i].message + '</font><br>';
// autoscroll to the new message
chat_div.scrollTop = chat_div.scrollHeight;
// update lastReceivedMessage
lastReceivedMessage = response[i].message_id;
}
}
}
// send a new message to the server
function sendMessage() {
if( sendReq ){
if(document.getElementById('message').value == '') {
return;
}
else {
// set the listener now for the response
sendReq.onreadystatechange = afterSend;
// Fill param: pass sender_id, sender_name, message
var param = "sender_name=" + document.getElementById("user_name").value;
param += "&message=" + escapeHtml( document.getElementById("message").value );
// Open and send request
sendReq.open( "POST", 'chatServer.php', true );
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.send(param);
// clear message input
document.getElementById("message").value = "";
}
}
}
function escapeHtml(text) {
var map = {
'&': ' plus ',
'+': ' plus ',
'<': '< ',
'>': ' >',
'"': '`'
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function afterSend() {
if( sendReq.readyState==4 && sendReq.status == 200) {
clearInterval(timer);
getUnreadMessages();
timer = setInterval("getUnreadMessages();", frequency);
}
}
function manageEnter() {
sendMessage();
return false;
}
function startChat() {
// set focus on input to start writing out of the box
document.getElementById('message').focus();
getUnreadMessages();
}