-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
49 lines (42 loc) · 1.33 KB
/
chat.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
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();
var mmm = $('#m').val();
socket.emit('notifyUser', user, mmm);
}
socket.on('chatMessage', function(from, msg){
var me = $('#user').val();
var color = (from == me) ? 'green' : '#009afd';
var from = (from == me) ? 'Me' : from;
$('#messages').append('<li><b style="color:' + color + '">' + from + '</b>: ' + msg + '</li>');
});
socket.on('notifyUser', function(user, mmm){
var me = $('#user').val();
if(user != me) {
$('#notifyUser').text(user + ' : ' + mmm);
}
setTimeout(function(){ $('#notifyUser').text(''); }, 50000);;
});
$(document).ready(function(){
var name =makeid();
$('#user').val(name);
socket.emit('chatMessage', 'System', '<b>' + name + '</b> has joined the discussion');
});
function makeid() {
var temp = prompt("Enter your name", "");
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return temp+"("+text+")";
}