-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (76 loc) · 1.84 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
var http = require('http'),
url = require('url'),
fs = require('fs'),
io = require('./lib/socket/lib/socket.io'),
sys = require('sys'),
irc=require('./lib/irc/irc');
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
},
server = http.createServer(function(req, res){
// your normal server code
var path = url.parse(req.url).pathname;
switch (path){
case '/':
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Welcome. Try the <a href="/client.html">chat</a> example.</h1>');
res.end();
break;
default:
if (/\.(js|html|swf|css)$/.test(path)){
try {
var swf = path.substr(-4) === '.swf';
res.writeHead(200, {'Content-Type': swf ? 'application/x-shockwave-flash' : ('text/' + (path.substr(-3) === '.js' ? 'javascript' : 'html'))});
res.write(fs.readFileSync(__dirname + path, swf ? 'binary' : 'utf8'), swf ? 'binary' : 'utf8');
res.end();
} catch(e){
send404(res);
}
break;
}
send404(res);
break;
}
});
server.listen(85);
io.listen(server, {
onClientConnect: function(client){
client.irc=new irc.Client();
client.irc.connect();
client.irc.addListener('Data',function(data){
var output={
'type':'Data',
'content':data
}
client.send(JSON.stringify(output));
})
},
onClientDisconnect: function(client){
client.irc.exit();
sys.puts('Disconnected Client!');
},
onClientMessage: function(message, client){
var obj=JSON.parse(message);
switch(obj.type){
case 'PING':
var output={
'type':'PING'
}
// sys.puts(client.sessionId+' Sent PING');
client.send(JSON.stringify(output));
break;
case 'Data':
sys.puts("Got: "+message)
client.irc.input(obj.content);
break;
case 'CONNECT':
break;
case 'DISCONNECT':
break;
default:
break;
}
}
});