-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
97 lines (87 loc) · 3.12 KB
/
server.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
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
var express = require('express'),
http = require('http'),
router = express(),
server = http.createServer(router),
jdenticon = require('jdenticon'),
fs = require('fs'),
md5 = require('js-md5'),
userObj = {},
activeUsers = [],
io = require('socket.io')(server),
dotenv = require('dotenv').config();
router.get('/',function(req,res){
res.sendFile(process.cwd()+'/public/index.html')
})
router.use(express.static('public'))
io.on('connection',function(socket){
var username;
console.log('a user connected');
socket.on('disconnect',function(){
console.log('user '+username+ ' is disconnecting')
delete userObj[username]
console.log(userObj)
if (activeUsers.indexOf(username) >= 0){
activeUsers.splice(activeUsers.indexOf(username),1);
console.log("currently active "+activeUsers)
}
io.emit('channel leave',username)
fs.access(process.cwd()+"/public/icons/"+encodeFilename(username)+".svg", fs.R_OK | fs.W_OK, function (err) {
console.log(err ? 'no access!' : 'can read/write');
if(!err){
fs.unlink(process.cwd()+"/public/icons/"+encodeFilename(username)+".svg",function(){
console.log('file '+encodeFilename(username)+'.svg has been deleted.')
})
}
});
})
socket.on('channel join',function(user){
while (activeUsers.indexOf(user) >=0 ) {
console.log('user already exists, creating alter ego')
user = user += Math.floor(Math.random()*100)
}
username = user;
userObj[username] = socket.id;
console.log(userObj)
activeUsers.push(user)
console.log('currently active '+activeUsers)
var hash = md5(username);
var size = 50;
var svg = jdenticon.toSvg(hash, size);
socket.emit('channel load',activeUsers);
fs.writeFile(process.cwd()+"/public/icons/"+encodeFilename(username)+".svg",svg,function(err){
if (err) console.log(err);
socket.broadcast.emit('channel join',user);
})
})
socket.on('text message',function(msg){
socket.broadcast.emit('text message',msg)
})
socket.on('whisper message',function(msg){
var recipient = msg.input[0];
msg.input.splice(0,1);
var message = msg.input.join(" ");
var socketid = userObj[recipient]
console.log(message);
var socketid = userObj[recipient];
if (io.sockets.connected[socketid]) {
console.log("socket found!");
io.sockets.connected[socketid].emit('whisper message',{username:msg.username, message:message});
}
})
})
server.listen(process.env.PORT, function(){
console.log('Server listening on port '+ process.env.PORT);
});
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length === 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
function encodeFilename(str){
return encodeURIComponent(str).replace(/[%\.]/gi,'_');
}