This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
150 lines (126 loc) · 3.94 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
/*
* Copyright 2011 Primary Technology Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Simple Node & Socket server
var http = require('http')
, url = require('url')
, fs = require('fs')
, io = require('socket.io')
, sys = require('sys')
, server;
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if(path == "/")
{
sendFile(res, path, __dirname + "/static/index.html");
}
else if(path.substring(0,"/static".length) == "/static")
{
sendFile(res, path, __dirname + path);
}
else if(path == "/newwall")
{
sendRedirect(res, path, "/" + randomWallName());
}
else
{
if(path.lastIndexOf("/")==0)
{
sendFile(res, path, __dirname + "/static/wall.html");
}
else
{
send404(res, path);
}
}
});
server.listen(80);
function randomWallName() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 10;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
function sendFile(res, reqPath, path)
{
path=path.replace("../","");
fs.readFile(path, function(err, data){
if (err){
send404(res, reqPath);
} else {
var contentType = "text/html";
if (path.substring(path.length -3, path.length) == ".js")
contentType = "text/javascript";
else if (path.substring(path.length -4, path.length) == ".css")
contentType = "text/css";
else if (path.substring(path.length -4, path.length) == ".gif")
contentType = "image/gif";
else if (path.substring(path.length -4, path.length) == ".png")
contentType = "image/png";
else if (path.substring(path.length -4, path.length) == ".ico")
contentType = "image/x-icon";
res.writeHead(200, {'Content-Type': contentType});
res.write(data, 'utf8');
res.end();
requestLog(200, reqPath, "-> " + path);
}
});
}
function send404(res, reqPath)
{
res.writeHead(404);
res.write("404 - Not Found");
res.end();
requestLog(404, reqPath, "NOT FOUND!");
}
function sendRedirect(res, reqPath, location)
{
res.writeHead(302, {'Location': location});
res.end();
requestLog(302, reqPath, "-> " + location);
}
function requestLog(code, path, desc)
{
console.log(code +", " + path + ", " + desc);
}
//var io = io.listen(server);
var io = require('socket.io').listen(server)
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set("polling duration", 10); // allow for long polling
io.set('log level', 1); // reduce logging
var messageHandler = require("./MessageHandler");
messageHandler.setSocketIO(io);
io.sockets.on('connection', function(client){
try{
messageHandler.handleConnect(client);
}catch(e){console.error(e);}
client.on('message', function(message){
try{
messageHandler.handleMessage(client, message);
}catch(e){console.error(e.message);}
});
client.on('disconnect', function(){
try{
messageHandler.handleDisconnect(client);
}catch(e){console.error(e);}
});
});