-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
48 lines (45 loc) · 1.46 KB
/
chat.js
File metadata and controls
48 lines (45 loc) · 1.46 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
var socket = io();
function submitfunction(){
var from = $('#user').val();
var message = $('#m').val();
if(message != '') {
socket.emit('chatMessage', from, message);
}
$('#m').val('').focus();
return false;
}
function notifyTyping() {
var user = $('#user').val();
socket.emit('notifyUser', user);
}
socket.on('chatMessage', function(from, msg){
var me = $('#user').val();
var color = (from == me) ? 'green' : '#009afd';
var from = (from == me) ? 'Me' : from;
if(from=='Me')
$('#messages').append('<li style="text-align:right;"><b style="color:' + color + '">' + from + '</b>: ' + msg + '</li>');
else
$('#messages').append('<li><b style="color:' + color + '">' + from + '</b>: ' + msg + '</li>');
});
//function to notify other user when typing..
socket.on('notifyUser', function(user){
var me = $('#user').val();
if(user != me) {
$('#notifyUser').text(user + ' is typing ...');
}
setTimeout(function(){ $('#notifyUser').text(''); }, 10000);;
});
$(document).ready(function(){
var name = makeid();
$('#user').val(name);
socket.emit('chatMessage', 'System', '<b>' + name + '</b> has joined the discussion');
});
// function to return a random username
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}