-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
212 lines (183 loc) · 6.44 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// No magic numbers
var textPort = 80,
rtcPort = 8080;
// All Module Imports
var _ = require('underscore'),
http = require('http'),
express = require('express'),
jade = require('jade'),
fs = require('fs'),
xmlstream = require('xml-stream'),
easyrtc = require('easyrtc'),
app = module.exports.app = express(),
rtcApp = module.exports.app = express(),
server = http.createServer(app),
rtcServer = http.createServer(rtcApp);
// Set up server file structure
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set("view options", { layout: false });
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('home.jade');
});
// Import words for room name generation
var words = new xmlstream(fs.createReadStream('xml/words.xml')),
rooms = [],
roomsWithUsers = {};
var adjectives = [],
colors = [],
nouns = [];
// Extract only words from the xml document
words.on('text: adjectives > word', function (item) {
adjectives.push(item['$text']);
});
words.on('text: colors > word', function (item) {
colors.push(item['$text']);
});
words.on('text: nouns > word', function (item) {
nouns.push(item['$text']);
});
// Set up socket.io server
var socketio = require('socket.io'),
io = socketio.listen(server),
rtcIO = socketio.listen(rtcServer);
// Server to client communication functions
/**
* Message expectations are defined by their prefixes.
* These prefixes are:
* universal - Transmit data to all clients, expect no response.
* broadcast - Transmit data to all clients in a given room, expect no response.
*/
/**
* Send a message to all sockets in a given room.
* @socket {socket} The socket sending the message.
* @message {string} The message being sent.
*/
function broadcastMessage(socket, message) {
var data = {'message': message,
'id': socket.id,
'nickname': socket.nickname};
socket.broadcast.to(socket.roomname).emit('broadcastMessage', data);
}
/**
* Send a notificiation to all sockets in a given room.
* @socket {socket} The socket about which the notification is about.
* @notification {string} The notification being snet.
*/
function broadcastNotification(socket, notification) {
var data = {'notification': notification};
socket.broadcast.to(socket.roomname).emit('broadcastNotification', data);
}
/**
* Notify all sockets in a given room that a user has joined the room.
* @socket {socket} The socket of the user that has joined the room.
*/
function broadcastUserJoined(socket) {
roomsWithUsers[socket.roomname].push([socket.id, socket.nickname]);
var data = {'nickname': socket.nickname,
'id': socket.id};
socket.broadcast.to(socket.roomname).emit('broadcastUserJoined', data);
}
/**
* Notify all sockets in a given room that a user has disconnected.
* @socket {socket} The socket of the use that has disconnected.
*/
function broadcastUserDisconnected(socket) {
roomsWithUsers[socket.roomname] = _.reject(roomsWithUsers[socket.roomname], function (item) {
return (item[0] === socket.id && item[1] === socket.nickname);
});
var data = {'nickname': socket.nickname,
'id': socket.id};
socket.broadcast.to(socket.roomname).emit('broadcastUserDisconnected', data);
}
/**
* Notify all sockets in a given room that a user has left the room.
* @socket {socket} the socket of the user that has left the room.
*/
function broadcastUserLeft(socket) {
roomsWithUsers[socket.roomname] = _.reject(roomsWithUsers[socket.roomname], function (item) {
return (item[0] === socket.id && item[1] === socket.nickname);
});
var data = {'nickname': socket.nickname,
'id': socket.id};
socket.broadcast.to(socket.roomname).emit('broadcastUserLeft', data);
}
// Client to server communication messages
io.sockets.on('connection', function (socket) {
/**
* Server has been asked to broadcast a message to a socket's room.
*/
socket.on('sendMessage', function (message) {
broadcastMessage(socket, message);
});
/**
* Server has been asked to delete a room that it created and was unused.
*/
socket.on('sendDeleteRoom', function (room) {
rooms = _.without(rooms, room);
});
/**
* Server has been asked to check if a room exists.
* Valid is true when the room exists, false when it does not.
*/
socket.on('checkRoom', function (name, fn) {
fn({'valid': rooms.indexOf(name) > -1});
});
/**
* Server has been asked to generate a new room.
* Response is set to the name of the new room.
*/
socket.on('getRoom', function (fn) {
var n = 'the-' + _.sample(adjectives) + '-' + _.sample(colors) + '-' + _.sample(nouns);
while (rooms.indexOf(n) > -1 || n.length > 32) {
n = 'the-' + _.sample(adjectives) + '-' + _.sample(colors) + '-' + _.sample(nouns);
}
rooms.push(n);
roomsWithUsers[n] = [];
fn({'response': n});
});
/**
* Server has been asked to send an array of users in a room.
* Response is set to an array of the users in the room.
*/
socket.on('getUsers', function (room, fn) {
fn({'response': roomsWithUsers[room]});
});
/**
* Server has been asked to move a socket to a room.
* Success is true when setting was successful, false when it failed.
*/
socket.on('setRoom', function (room, fn) {
socket.join(room);
socket.roomname = room;
broadcastUserJoined(socket);
fn({'success': true});
});
/**
* Server has been asked to a set a socket's name.
* Success is true when setting was successful, false when it failed.
*/
socket.on('setNickname', function (data, fn) {
socket.nickname = data;
fn({'success': true, 'userID': socket.id});
});
/**
* Server has been notified of a user disconnecting.
*/
socket.on('disconnect', function () {
broadcastUserDisconnected(socket);
});
});
// WebRTC configuration
easyrtc.roomDefaultEnable = false;
// Start server, and tell us it's running
server.listen(textPort);
rtcServer.listen(rtcPort);
var easyrtcServer;
// Check if started debug mode before starting RTC server
if (process.argv[2] === '0') {
easyrtcServer = easyrtc.listen(rtcApp, rtcIO, {logLevel:"debug", logDateEnable:true});
} else {
easyrtcServer = easyrtc.listen(rtcApp, rtcIO);
}