-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
127 lines (102 loc) · 2.74 KB
/
index.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
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
127
var express = require('express');
var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var url = require('url');
var fs = require('fs');
var rooms = {};
/* Routes */
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/chatroom/:name', function(req, res){
if(!req.params.name || req.params.name.trim() === '')
res.redirect('/');
res.sendFile(__dirname + '/chatroom.html');
});
app.get('*', function(req, res){
res.sendFile(__dirname + '/404.html');
});
/* Server Events*/
io.on('connection', function(socket){
/* create new room event */
socket.on('join room', function(room, userName) {
if(!room || room.trim() === '') {
socket.disconnect(true);
return;
}
if(!userName || userName.trim().length === 0) {
socket.disconnect(true);
return false;
}
if(userNameExists(room, userName)) {
socket.emit('username exists');
return;
}
socket.join(room, () => {
insertUser(room, userName);
io.sockets.to(room).emit('room users', roomUsers(room));
io.sockets.to(room).emit('user joined', userName);
});
socket.to(room).on('chat message', function(data){
io.sockets.in(room).emit('chat message', data);
});
socket.to(room).on('disconnect', function(){
//console.log("you are disconnected in room", room);
removeUser(room, userName);
socket.leave(room);
io.sockets.to(room).emit('user left', userName);
io.sockets.to(room).emit('room users', roomUsers(room));
});
});
});
/* Helpers */
function insertUser(room, userName) {
//console.log('insert before', rooms);
if(!userNameExists(room, userName)) {
if(room in rooms) {
rooms[room].users.push(userName);
} else {
rooms[room] = {users: [userName]};
}
}
//console.log('insert', rooms);
}
function removeUser(room, userName) {
if(userNameExists(room, userName)) {
if(room in rooms) {
let pos = rooms[room].users.indexOf(userName);
rooms[room].users.splice(pos, 1);
}
}
//console.log("remove", rooms);
}
function userNameExists(room, userName) {
if(room in rooms) {
if(rooms[room].users.indexOf(userName) !== -1) {
//console.log(userName, 'exists');
return true;
}
}
return false;
}
function roomExists(room) {
if(room in rooms) {
return true;
}
return false;
}
function roomUsers(room) {
if(!room || room.trim().length === 0) {
return null;
}
if(room in rooms) {
return rooms[room]['users'];
}
}
/* Start Listening for connections */
http.listen(3000, function(){
//console.log('listening on *:3000');
});