-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
170 lines (140 loc) · 4.04 KB
/
server.js
File metadata and controls
170 lines (140 loc) · 4.04 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
// var HashTable = require('hashtable');
// var users = new HashTable();
var users = [];
var connections = [];
var rooms = [];
var roomCounter = [];
app.use(express.static('public'));
server.listen(process.env.PORT || 3000);
console.log("running!");
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on("connection", function(socket){
connections.push(socket);
console.log("Sockets connected %s", connections.length);
updateAllUsers();
// Chat Message
socket.on('chat message', function(data){
if(!socket.user) { socket.user = 'anonymous coward';}
if(!socket.location) {returnToLobby(socket);}
var msg = {"user":socket.user, "message":data.message};
io.to(socket.location).emit('new message',msg);
// console.log(socket.rooms);
});
// Disconnect
socket.on('disconnect', function(data){
connections.splice(connections.indexOf(socket),1);
console.log('Disconnected: %s sockets connected', connections.length);
if(socket.location)
roomCount(socket.location, false);
if(socket.user){
users.splice(users.indexOf(socket.user), 1);
updateAllUsers();
}
});
// New User
socket.on('new user', function(data, callback){
if(!data){
callback(false);
return;
}
newUser(socket,data);
returnToLobby(socket);
callback(true); // I will throw room_array in here, instead of updating
});
function newUser(socket,name){
socket.user = name;
users.push(socket.user);
updateAllUsers();
updateRooms();
}
// Make Room
socket.on('make room', function(data,callback){
if(!data.title || rooms.indexOf(data.title) > -1){
callback(false);
return;
// either no title, or the room name is taken
}
joinRoom(socket, data.title);
rooms.push(socket.location);
roomCounter.push(0); // ?
updateRooms();
callback(true);
});
// Join Room
socket.on('join room', function(data,callback){
if(!data.title || rooms.indexOf(data.title) == -1 ){
callback(false);
return;
}
joinRoom(socket, data.title);
callback(true);
});
// Return to Lobby
socket.on('lobby', function(callback){
returnToLobby(socket);
callback();
});
//// client needs a (return to lobby) button /////
function returnToLobby(socket){
if(!socket.user){ newUser(socket,'Anonymous Coward');}
if(socket.location)
{
var msg = {"user":socket.user, "exit":true};
io.to(socket.location).emit('new message',msg); /// announces leave
socket.leave(socket.location);
roomCount(socket.location, false);
// socket.join(socket.location); // joins lobby channel
}
socket.location = 'lobby';
}
function updateRooms(){ io.sockets.emit('get rooms', rooms); }// will update (number of people)
function updateAllUsers(){
var data = {"allUsers" : true, "users":users};
io.sockets.emit('get users', data);
}
function updateRoomUsers(roomname){
var data = {};
data.allUsers = false;
var roomUsers = [];
var room = io.sockets.adapter.rooms[roomname]; // room object
// console.log(room);
for(var id in room.sockets)
roomUsers.push(io.sockets.connected[id].user);
data.users = roomUsers;
io.to(roomname).emit('get users', data);
// io.sockets.connected has every fcking socket as a value to an ID
}
function joinRoom(socket, roomname){
returnToLobby(socket);
socket.location = roomname;
socket.join(socket.location, function(){
roomCount(socket.location, true);
updateRoomUsers(roomname); // jeez
var msg = {"user":socket.user, "enter":true};
io.to(socket.location).emit('new message',msg);
});
}
function roomCount(roomname, increment){
var index = rooms.indexOf(roomname);
if(index == -1){return;} // room not exist
else{
if(!increment){
roomCounter[index]--;
if(roomCounter[index]<=0){ // destroy room
rooms.splice(index, 1);
roomCounter.splice(index, 1);
}
updateRooms();
}
else{
roomCounter[index]++;
}
}
}
});