forked from akavlie/web-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (55 loc) · 2.19 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
var express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(app),
irc = require('irc');
app.configure(function() {
app.use(express.static(__dirname + '/'));
app.use(express.static(__dirname + '/js'));
app.use(express.static(__dirname + '/images'));
app.listen(8337);
});
app.configure('production', function() {
app.listen(12445);
});
console.log('Express server started on port %s', app.address().port);
// Socket.IO
io.sockets.on('connection', function(socket) {
// Events to signal TO the front-end
var events = {
'join': ['channel', 'nick'],
'part': ['channel', 'nick'],
'nick': ['oldNick', 'newNick', 'channels'],
'names': ['channel', 'nicks'],
'message': ['from', 'to', 'text'],
'pm': ['nick', 'text'],
'motd': ['motd'],
'error': ['message']
};
socket.on('connect', function(data) {
var client = new irc.Client(data.server, data.nick, {
channels: data.channels
});
// Socket events sent FROM the front-end
socket.on('join', function(name) { client.join(name); });
socket.on('part', function(name) { client.part(name); });
socket.on('say', function(data) { client.say(data.target, data.message); });
socket.on('command', function(text) { console.log(text); client.send(text); });
socket.on('disconnect', function() { client.disconnect(); });
// Add a listener on client for the given event & argument names
var activateListener = function(event, argNames) {
client.addListener(event, function() {
console.log('Event ' + event + ' sent');
// Associate specified names with callback arguments
// to avoid getting tripped up on the other side
var callbackArgs = arguments;
args = {};
argNames.forEach(function(arg, index) {
args[arg] = callbackArgs[index];
});
console.log(args);
socket.emit(event, args);
});
};
for (var event in events) { activateListener(event, events[event]); }
});
});