-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
41 lines (37 loc) · 1.22 KB
/
client.js
File metadata and controls
41 lines (37 loc) · 1.22 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
/** @author Jørn Kinderås */
(function() {
"use strict";
var socket, messageList, sendBtn, messageInput;
/** Initiate the socket connection with the current URL */
socket = io.connect(document.location.origin);
/** Grab all DOM elements */
messageList = document.getElementById('messageList');
sendBtn = document.getElementById('sendBtn');
messageInput = document.getElementById('messageInput');
/** Listen for any messages from the server */
socket.on('message', function(data) {
// If the payload has a message
if (data.message) {
// Create a new list element
// and push it to the chat list
var msg = document.createElement('li');
msg.innerHTML = (data.time || "") + " - " + data.message;
messageList.insertBefore(msg, messageList.firstChild);
}
});
/** Add an event listener to the send button */
sendBtn.addEventListener('click', function(e) {
var d = new Date();
var time = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
// Send a message to the server
// when the user clicks the send button
var message = messageInput.value || "";
// ..if there is any content
if (message.length > 0) {
socket.emit('send', {
"message": messageInput.value,
time: time
});
}
});
}());