From 0c64b536c8abc5f8616e99b00f4e17bfaa1c5f97 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 6 Dec 2010 19:10:44 +0100 Subject: [PATCH] nedap websockets work --- nedap/server.js | 42 ++++++++++++++++++++++++------------------ quiz.js | 4 +++- server.js | 29 ++++++++++++++++------------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/nedap/server.js b/nedap/server.js index c6cf2cb..8adc886 100644 --- a/nedap/server.js +++ b/nedap/server.js @@ -2,7 +2,7 @@ var Connect = require('connect'); var wss = require('websocket-server'); var ltx = require('ltx'); -var WS_PROTOCOL = 'nedap-kneemFothbedchoadHietEnobKavLub1'; +var WS_KEY = 'nedap-kneemFothbedchoadHietEnobKavLub1'; var backend, question, answers, scores; @@ -19,11 +19,12 @@ function html(body) { function nedap(app) { app.get('/', function(req, res) { if (question && answers) { +console.log({question:question,answers:answers}) var form = new ltx.Element('form', { action: '/', method: 'POST', enctype: 'application/x-www-form-urlencoded' }); - form.c('p').text(question); + form.c('p').t(question); var ul = form.c('ul'); for(var i = 0; i < answers.length; i++) { ul.c('li'). @@ -32,7 +33,7 @@ function nedap(app) { name: 'a', value: ''+i }). c('label', { for: 'a'+i }). - t(answers[i]); + t(answers[i].text); } form.c('input', { type: 'submit', value: 'Submit' }); @@ -79,11 +80,25 @@ var server = Connect.createServer( ); wss.createServer({ server: server }).on('connection', function(conn) { -console.log(conn); - if (conn.protocol === WS_PROTOCOL) { - backend = conn; + var authed = false; - conn.on('message', function(data) { + conn.on('message', function(data) { + if (!authed) { + if (data.toString() === WS_KEY) { + console.warn('Authorized WebSocket'); + backend = conn; + authed = true; + + var reset = function() { + backend = null; + }; + conn.on('close', reset); + conn.on('error', reset); + } else { + console.warn('Unauthorized backend WebSocket'); + conn.close(); + } + } else { try { var msg = JSON.parse(data); console.log({msg: msg}); @@ -102,17 +117,8 @@ console.log(conn); } catch (e) { console.error(e.stack); } - }); - - var reset = function() { - backend = null; - }; - conn.on('close', reset); - conn.on('error', reset); - } else { - console.error({ 'Wrong Protocol': conn.protocol }); - conn.end(); - } + } + }); }); server.listen(8080); /* TODO: port 80 */ diff --git a/quiz.js b/quiz.js index afc5052..ec700cb 100644 --- a/quiz.js +++ b/quiz.js @@ -50,7 +50,7 @@ function loadQuizData(done) { } var url = 'ws://' + document.location.host + '/'; -var ws = new WebSocket(url, 'quiz'); +var ws = new WebSocket(url, '*'); ws.onerror = function(e) { console.error(e.message); @@ -61,6 +61,7 @@ ws.onclose = function() { var onBackendMessage; ws.onmessage = function(data) { try { + console.log('fromBackend: ' + data); var msg = JSON.parse(data); if (onBackendMessage) onBackendMessage(msg); @@ -69,6 +70,7 @@ ws.onmessage = function(data) { } }; var sendToBackend = function(msg) { + console.log('toBackend: ' + JSON.stringify(msg)); ws.send(JSON.stringify(msg)); }; ws.onopen = function() { diff --git a/server.js b/server.js index d5a5f85..cfe02d5 100644 --- a/server.js +++ b/server.js @@ -5,9 +5,10 @@ var wsc = require('websocket-client'); var frontend; /* TODO: url */ -var nedap = new wsc.WebSocket('ws://localhost:8080/', 'nedap-kneemFothbedchoadHietEnobKavLub1'); +var nedap = new wsc.WebSocket('ws://localhost:8080/', 'quiz-nedap'); nedap.onopen = function() { console.log('NEDAP opened'); + nedap.send('nedap-kneemFothbedchoadHietEnobKavLub1'); }; nedap.onclose = function() { console.log('NEDAP closed'); @@ -33,25 +34,27 @@ var server = Connect.createServer( ); wss.createServer({ server: server }).on('connection', function(conn) { - if (conn.protocol === 'quiz') { - frontend = conn; + frontend = conn; - conn.on('data', function(data) { + conn.on('message', function(data) { + console.log({data:data}); + try { var msg = JSON.parse(data); if (msg.nedap) { console.log({ toNedap: msg.nedap }); nedap.send(JSON.stringify(msg.nedap)); } - }); + + } catch (e) { + console.error(e.stack); + } + }); - var reset = function() { - frontend = null; - }; - conn.on('close', reset); - conn.on('error', reset); - } else { - conn.end(); - } + var reset = function() { + frontend = null; + }; + conn.on('close', reset); + conn.on('error', reset); }); server.listen(8081);