diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | server.js | 92 |
2 files changed, 6 insertions, 88 deletions
@@ -1 +1 @@ | |||
node_modules | .nodester.appconfig | ||
@@ -1,87 +1,5 @@ | |||
1 | var server = require('http').createServer(handler), | 1 | var http = require('http'); |
2 | io = require('socket.io').listen(server), | 2 | http.createServer(function (req, res) { |
3 | fs = require('fs'), | 3 | res.writeHead(200, {'Content-Type': 'text/plain'}); |
4 | url = require('url'), | 4 | res.end('Hello World\nApp (web-irc) is running..'); |
5 | irc = require('irc'); | 5 | }).listen(12445); |
6 | |||
7 | var HOST = 'localhost', | ||
8 | PORT = 8337; | ||
9 | |||
10 | // HTTP handler | ||
11 | function handler(req, res) { | ||
12 | path = url.parse(req.url).pathname; | ||
13 | if (path === '/') path = '/index.html' | ||
14 | |||
15 | // Set content type | ||
16 | var type = ''; | ||
17 | if (path.substr(-3) === '.js') | ||
18 | type = 'text/javascript'; | ||
19 | else if (path.substr(-4) === '.css') | ||
20 | type = 'text/css'; | ||
21 | else if (path.substr(-4) === '.png') | ||
22 | type = 'image/png'; | ||
23 | else | ||
24 | type = 'text/html'; | ||
25 | |||
26 | fs.readFile(__dirname + path, function (err, data) { | ||
27 | if (err) { | ||
28 | res.writeHead(500); | ||
29 | return res.end('Error loading index.html'); | ||
30 | } | ||
31 | |||
32 | res.writeHead(200, {'Content-Type': type}); | ||
33 | res.end(data); | ||
34 | }); | ||
35 | } | ||
36 | |||
37 | server.listen(PORT, HOST); | ||
38 | console.log('Server running at http://' + HOST + ':' + PORT); | ||
39 | |||
40 | |||
41 | // Socket.IO | ||
42 | io.sockets.on('connection', function(socket) { | ||
43 | |||
44 | // Events to signal TO the front-end | ||
45 | var events = { | ||
46 | 'join': ['channel', 'nick'], | ||
47 | 'part': ['channel', 'nick'], | ||
48 | 'nick': ['oldNick', 'newNick', 'channels'], | ||
49 | 'names': ['channel', 'nicks'], | ||
50 | 'message': ['from', 'to', 'text'], | ||
51 | 'pm': ['nick', 'text'], | ||
52 | 'motd': ['motd'], | ||
53 | 'error': ['message'] | ||
54 | }; | ||
55 | |||
56 | socket.on('connect', function(data) { | ||
57 | var client = new irc.Client(data.server, data.nick, { | ||
58 | channels: data.channels | ||
59 | }); | ||
60 | |||
61 | // Socket events sent FROM the front-end | ||
62 | socket.on('join', function(name) { client.join(name); }); | ||
63 | socket.on('part', function(name) { client.part(name); }); | ||
64 | socket.on('say', function(data) { client.say(data.target, data.message); }); | ||
65 | socket.on('command', function(text) { console.log(text); client.send(text); }); | ||
66 | socket.on('disconnect', function() { client.disconnect(); }); | ||
67 | |||
68 | |||
69 | // Add a listener on client for the given event & argument names | ||
70 | var activateListener = function(event, argNames) { | ||
71 | client.addListener(event, function() { | ||
72 | console.log('Event ' + event + ' sent'); | ||
73 | // Associate specified names with callback arguments | ||
74 | // to avoid getting tripped up on the other side | ||
75 | var callbackArgs = arguments; | ||
76 | args = {}; | ||
77 | argNames.forEach(function(arg, index) { | ||
78 | args[arg] = callbackArgs[index]; | ||
79 | }); | ||
80 | console.log(args); | ||
81 | socket.emit(event, args); | ||
82 | }); | ||
83 | }; | ||
84 | |||
85 | for (var event in events) { activateListener(event, events[event]); } | ||
86 | }); | ||
87 | }); \ No newline at end of file | ||