2010-12-04 22:04:58 +01:00
|
|
|
var Connect = require('connect');
|
2010-12-06 18:01:48 +01:00
|
|
|
var wss = require('websocket-server');
|
|
|
|
var wsc = require('websocket-client');
|
2010-12-10 22:04:17 +01:00
|
|
|
var irc = require('irc-js');
|
2010-12-04 22:04:58 +01:00
|
|
|
|
|
|
|
var frontend;
|
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Nedap backend connection
|
|
|
|
*/
|
|
|
|
|
2010-12-04 22:04:58 +01:00
|
|
|
/* TODO: url */
|
2010-12-06 19:19:52 +01:00
|
|
|
var nedap;
|
|
|
|
function connectNedap() {
|
2010-12-20 04:45:07 +01:00
|
|
|
nedap = new wsc.WebSocket('ws://nedap.c3d2.de/', 'quiz-nedap');
|
2010-12-06 19:19:52 +01:00
|
|
|
nedap.onopen = function() {
|
|
|
|
console.log('NEDAP opened');
|
|
|
|
nedap.send('nedap-kneemFothbedchoadHietEnobKavLub1');
|
|
|
|
};
|
|
|
|
nedap.onclose = function() {
|
|
|
|
console.log('NEDAP closed');
|
|
|
|
connectNedap();
|
|
|
|
};
|
|
|
|
nedap.onerror = function(e) {
|
|
|
|
console.log('NEDAP error: ' + e.message);
|
|
|
|
connectNedap();
|
|
|
|
};
|
|
|
|
nedap.onmessage = function(data) {
|
|
|
|
try {
|
|
|
|
var msg = JSON.parse(data);
|
|
|
|
console.log({ fromNedap: msg });
|
2010-12-10 22:04:17 +01:00
|
|
|
sendToFrontend({ nedap: msg });
|
2010-12-06 19:19:52 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.stack);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
connectNedap();
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IRC client
|
|
|
|
*/
|
|
|
|
|
|
|
|
var IRC_SERVER = 'irc.freenode.net';
|
|
|
|
var IRC_CHAN = '#pentanews';
|
|
|
|
var chat = new irc({ server: IRC_SERVER,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
nick: '[Ceiling]Cat'
|
|
|
|
});
|
|
|
|
function connectChat() {
|
|
|
|
chat.connect();
|
|
|
|
}
|
|
|
|
connectChat();
|
|
|
|
chat.addListener('376', function() {
|
|
|
|
chat.join(IRC_CHAN);
|
|
|
|
});
|
|
|
|
chat.addListener('366', function(msg) {
|
|
|
|
if (msg.params[1] === IRC_CHAN) {
|
|
|
|
console.log('Successfully joined ' + IRC_CHAN);
|
|
|
|
pushIrcInfo();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chat.addListener('privmsg', function(msg) {
|
|
|
|
console.log({PRIVMSG:msg});
|
|
|
|
var nick = msg.person.nick;
|
|
|
|
var channel = msg.params[0];
|
|
|
|
var text = msg.params[1];
|
|
|
|
if (nick && channel === IRC_CHAN && text && frontend) {
|
|
|
|
sendToFrontend({ irc: { nick: nick,
|
|
|
|
text: text
|
|
|
|
} });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chat.addListener('disconnected', function() {
|
|
|
|
console.error('Chat disconnected!');
|
|
|
|
process.nextTick(connectChat);
|
|
|
|
});
|
|
|
|
|
|
|
|
function pushIrcInfo() {
|
|
|
|
sendToFrontend({ irc: { server: IRC_SERVER,
|
|
|
|
channel: IRC_CHAN } });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Web server
|
|
|
|
*/
|
|
|
|
|
2010-12-04 22:04:58 +01:00
|
|
|
var server = Connect.createServer(
|
|
|
|
Connect.logger(),
|
|
|
|
Connect.bodyDecoder(),
|
|
|
|
Connect.staticProvider(__dirname),
|
|
|
|
Connect.errorHandler({ dumpExceptions: true, showStack: true })
|
|
|
|
);
|
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
/*
|
|
|
|
* WebSocket server
|
|
|
|
*/
|
|
|
|
|
2010-12-06 18:01:48 +01:00
|
|
|
wss.createServer({ server: server }).on('connection', function(conn) {
|
2010-12-06 19:10:44 +01:00
|
|
|
frontend = conn;
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2010-12-06 19:10:44 +01:00
|
|
|
conn.on('message', function(data) {
|
|
|
|
console.log({data:data});
|
|
|
|
try {
|
2010-12-04 22:04:58 +01:00
|
|
|
var msg = JSON.parse(data);
|
2010-12-04 23:08:05 +01:00
|
|
|
if (msg.nedap) {
|
|
|
|
console.log({ toNedap: msg.nedap });
|
2010-12-04 22:04:58 +01:00
|
|
|
nedap.send(JSON.stringify(msg.nedap));
|
2010-12-04 23:08:05 +01:00
|
|
|
}
|
2010-12-10 22:04:17 +01:00
|
|
|
else if (msg.irc === "activate") {
|
|
|
|
pushIrcInfo();
|
|
|
|
}
|
2010-12-06 19:10:44 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.stack);
|
|
|
|
}
|
|
|
|
});
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2010-12-06 19:10:44 +01:00
|
|
|
var reset = function() {
|
|
|
|
frontend = null;
|
|
|
|
};
|
|
|
|
conn.on('close', reset);
|
|
|
|
conn.on('error', reset);
|
2010-12-04 22:04:58 +01:00
|
|
|
});
|
2010-12-06 18:01:48 +01:00
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
function sendToFrontend(obj) {
|
|
|
|
if (!frontend)
|
|
|
|
return;
|
|
|
|
|
|
|
|
frontend.send(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
|
2010-12-06 18:01:48 +01:00
|
|
|
server.listen(8081);
|