forked from jkingsman/FreeStep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (134 loc) · 4.62 KB
/
server.js
File metadata and controls
158 lines (134 loc) · 4.62 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
var express = require('express'),
https = require("https"),
fs = require('fs');
/***
*
* This is for HTTP redirecting to HTTPS - if you're running this as HTTP, delete below until the closing comment block
*
***/
var http = express();
// set up a route to redirect http to https
http.get('*',function(req,res){
res.redirect('https://freestep.net')
})
// have it listen on 80
http.listen(80);
/***
*
* This is for HTTP redirecting to HTTPS - if you're running this as HTTP, delete above until the opening comment block
*
***/
var privateKey = fs.readFileSync('ssl/server.key').toString();
var certificate = fs.readFileSync('ssl/freestep_net.crt').toString();
var ca = fs.readFileSync('ssl/COMODO.ca-bundle').toString();
var sslOptions = {
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/freestep_net.crt'),
ca: fs.readFileSync('ssl/COMODO.ca-bundle')
};
var app = express();
var server = https.createServer(sslOptions, app);
var io = require("socket.io").listen(server);
app.configure(function () {
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 443);
app.set('ipaddr', process.env.OPENSHIFT_NODEJS_IP || "freestep.net");
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));
});
//lets us get room memebers in socket.io >=1.0
function findClientsSocketByRoomId(roomId) {
var res = [],
room = io.sockets.adapter.rooms[roomId];
if(room) {
for(var id in room) {
res.push(io.sockets.adapter.nsp.connected[id]);
}
}
return res;
}
app.get('/', function (req, res) {
res.render('index.html');
});
server.listen(app.get('port'), app.get('ipaddr'), function () {
console.log('Express server listening on ' + app.get('ipaddr') + ':' + app.get('port'));
});
io.sockets.on("connection", function (socket) {
var lastImageSend = 0;
var isRateLimited = 1;
//emits newuser
socket.on("joinReq", function (name, room, password) {
var roomID = room + password;
var roomPassword = password;
var allowJoin = 1;
var denyReason = null;
//get the client list and push it to the client
var clientsInRoom = findClientsSocketByRoomId(roomID);
var clientsInRoomArray = [];
var clientsInRoomArrayScrubbed = [];
clientsInRoom.forEach(function (client){
clientsInRoomArray.push(client.nickname);
/* the scrubbed one has the sanitized version as used in the username
* classes for typing, so we can check and avoid collisions.
*/
clientsInRoomArrayScrubbed.push(client.nickname.replace(/\W/g, ''));
});
//check if the nickname exists
if(clientsInRoomArrayScrubbed.indexOf(name.replace(/\W/g, '')) != -1) {
denyReason = "Nickname is already taken.";
allowJoin = 0;
}
if(allowJoin) {
//now in the room
socket.join(roomID);
socket.nickname = name;
socket.roomIn = roomID;
//send the join confirmation to the client, alert the room, and push a user list
socket.emit("joinConfirm");
//add them to the user list since they're now a member
clientsInRoomArray.push(socket.nickname);
io.sockets.in(roomID).emit("newUser", name);
socket.emit("userList", clientsInRoomArray);
} else {
socket.emit("joinFail", denyReason);
}
});
//emits goneuser
socket.on('disconnect', function () {
io.sockets.in(socket.roomIn).emit("goneUser", socket.nickname);
});
//emits chat
socket.on("textSend", function (msg) {
//0 = text
var type = 0;
var name = socket.nickname;
var data = [type, name, msg];
io.sockets.in(socket.roomIn).emit("chat", data);
});
//lets admins un-ratelimit themselves for data
socket.on("unRateLimit", function () {
isRateLimited = 0;
});
//emits data
socket.on("dataSend", function (msg) {
var currTime = Date.now();
if(((currTime - lastImageSend) < 5000) && isRateLimited) {
//it's been less than five seconds; no data for you!
socket.emit("rateLimit");
} else {
lastImageSend = currTime;
//1 = image
var type = 1;
var name = socket.nickname;
var data = [type, name, msg];
io.sockets.in(socket.roomIn).emit("chat", data);
}
});
//emits typing
socket.on("typing", function (typing) {
io.sockets.in(socket.roomIn).emit("typing", [typing, socket.nickname]);
});
});