-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
136 lines (121 loc) · 3.32 KB
/
server.js
File metadata and controls
136 lines (121 loc) · 3.32 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
var http = require('http'),
url = require('url'),
fs = require('fs'),
io = require('socket.io'),
sys = require('sys'),
//wikipedia = require('./lib/wikipedia'),
twitter = require("./lib/twitter"),
sapofotos = require("./lib/sapofotos"),
sapovideos = require("./lib/sapovideos"),
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if (path == '/'){
fs.readFile(__dirname +'/public/index.html', function(err, data){
if (err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(data, 'utf8');
res.end();
});
}
else if (path == '/chat'){
fs.readFile(__dirname +'/public'+path+'.html', function(err, data){
if (err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(data, 'utf8');
res.end();
});
}
else if (path.match(/\.js$/)){
// get the js files from the js directory
// this must be evaluated for potential security issues
fs.readFile(__dirname + '/public/js' + path, function(err, data){
if (err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/javascript' })
res.write(data, 'utf8');
res.end();
});
}
else if (path.match(/\.css$/)){
fs.readFile(__dirname + '/public/css' + path, function(err, data){
if (err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/css' })
res.write(data, 'utf8');
res.end();
});
}
else if (path.match(/\.(jpg|jpeg|png|gif)$/)){
fs.readFile(__dirname + '/public/imgs' + path, function(err, data){
if (err) return send404(res);
var tmpExt = path.substr(-3);
if (tmpExt == 'jpg' || 'jpeg'){
var extType = 'jpeg';
}
else if (tmpExt == 'gif'){
var extType = 'gif'
}
else if (tmpExt == 'png'){
var extType = 'png';
}
else{
send404();
}
res.writeHead(200, {'Content-Type': 'image/'+extType })
res.write(data, 'utf8');
res.end();
});
}
else{
send404(res);
}
});
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
};
server.listen(8080, {
transportOptions: {
'xhr-polling': {
closeTimeout: 1000 * 60 * 5
}
}
});
// socket.io, I choose you
// simplest chat application evar
var io = io.listen(server),
buffer = [];
io.sockets.on('connection', function(client){
client.json.send({ buffer: buffer });
client.json.broadcast.send({ announcement: client.sessionId + ' connected' });
client.on('message', function(message){
var msg = { message: [client.sessionId, message] };
buffer.push(msg);
if (buffer.length > 15) buffer.shift();
client.json.broadcast.send(msg);
sys.puts("txt to search: " + message);
if (message){
var aux = message.split(' ');
var txtConcat = "";
for (var i=0; i < aux.length ; i++){
if (aux[i] && i!=aux.length-1){
txtConcat += aux[i]+"%20";
}
else if (aux[i] && i==aux.length-1){
txtConcat += aux[i];
}
}
var txt = aux[aux.length-1];
sys.puts("txt: " + txt);
sys.puts("txtConcat: " + txtConcat);
twitter(txt,client);
sapofotos(txt,client);
sapovideos(txt,client);
}
});
client.on('disconnect', function(){
client.json.broadcast.send({ announcement: client.sessionId + ' disconnected' });
});
client.on('draw', function(){
client.json.broadcast.send({ announcement: client.sessionId + ' drawn something' });
});
});