-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (65 loc) · 2.06 KB
/
app.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
//Globals and Includes
var PORT = 9001
express = require('express')
app = express()
irc = require('irc');
//Express config
app.set('views',__dirname+"/views");
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express);
app.use(express.static(__dirname+"/public"));
//Accept only one route
app.get('/', function(req,res){
res.render("home");
});
//Include Socket.io, listen over express
var io = require('socket.io').listen(app.listen(PORT));
//Socket.io magic
io.sockets.on('connection', function(socket){
//Instance Variables for socket connection
var client;
var channel;
var server;
var nick;
//when client requests to join
socket.on('ircconnect', function(data){
server = data.server;
channel = data.channel;
nick = data.nick;
//Log server info for debugging
console.log("attempting to join : "+data.server+" "+data.channel+" as "+data.nick);
//Join irc server + specific channel
client = new irc.Client(server, nick, {channels: [channel]});
//Listener for when successfully joined
client.on('join',function(channel, nick){
//Send join notification
socket.emit('message', {message: nick+' joined '+server+' -> '+channel});
});
//Listener for other irc clients in channel
client.on('names',function(channel, nicks){
//send list of other clients in channel to client
socket.emit('nicklist',{nicklist: nicks});
});
//Add listener for messages in irc
client.addListener('message', function(from, to, msg){
//on message, send to client
socket.emit('message', {from: from, to: to, msg: msg});
});
//Listener for when client wants to send message
socket.on('send',function(data){
//send message through irc
client.say(channel,data.message);
});
//Listener for when client disconnects from socket
socket.on('disconnect',function(){
//leave irc server
client.disconnect();
});
//Listener for manual disconnect
socket.on('userdisconnect',function(){
client.disconnect();
});
});
});
//Log the port that the app is listening on
console.log("App listening on port -> "+PORT);