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-04 22:04:58 +01:00
|
|
|
|
|
|
|
var frontend;
|
|
|
|
|
|
|
|
/* TODO: url */
|
2010-12-06 18:01:48 +01:00
|
|
|
var nedap = new wsc.WebSocket('ws://localhost:8080/', 'nedap-kneemFothbedchoadHietEnobKavLub1');
|
2010-12-04 22:04:58 +01:00
|
|
|
nedap.onopen = function() {
|
|
|
|
console.log('NEDAP opened');
|
|
|
|
};
|
|
|
|
nedap.onclose = function() {
|
|
|
|
console.log('NEDAP closed');
|
|
|
|
};
|
|
|
|
nedap.onerror = function(e) {
|
|
|
|
console.log('NEDAP error: ' + e.message);
|
|
|
|
};
|
|
|
|
nedap.onmessage = function(data) {
|
|
|
|
try {
|
|
|
|
var msg = JSON.parse(data);
|
2010-12-04 23:08:05 +01:00
|
|
|
console.log({ fromNedap: msg });
|
2010-12-04 22:04:58 +01:00
|
|
|
frontend.send(JSON.stringify({ nedap: msg }));
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.stack);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var server = Connect.createServer(
|
|
|
|
Connect.logger(),
|
|
|
|
Connect.bodyDecoder(),
|
|
|
|
Connect.staticProvider(__dirname),
|
|
|
|
Connect.errorHandler({ dumpExceptions: true, showStack: true })
|
|
|
|
);
|
|
|
|
|
2010-12-06 18:01:48 +01:00
|
|
|
wss.createServer({ server: server }).on('connection', function(conn) {
|
2010-12-04 22:04:58 +01:00
|
|
|
if (conn.protocol === 'quiz') {
|
|
|
|
frontend = conn;
|
|
|
|
|
|
|
|
conn.on('data', function(data) {
|
|
|
|
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-04 22:04:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var reset = function() {
|
|
|
|
frontend = null;
|
|
|
|
};
|
|
|
|
conn.on('close', reset);
|
|
|
|
conn.on('error', reset);
|
|
|
|
} else {
|
|
|
|
conn.end();
|
|
|
|
}
|
|
|
|
});
|
2010-12-06 18:01:48 +01:00
|
|
|
|
|
|
|
server.listen(8081);
|