Skip to content

Commit e199d62

Browse files
committed
custom html, poll variables, server-side events
1 parent 2224620 commit e199d62

File tree

2 files changed

+139
-42
lines changed

2 files changed

+139
-42
lines changed

lib.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
{ http: ( {
22
queue : [],
3-
changeView : false,
43
loadExpected : false,
54
showKeys : false,
65
intervals : [],
6+
currentDisplay : "",
7+
buttonTimer : "",
8+
eventStream : {},
9+
10+
establishSSE : ( function establishSSE( ) {
11+
lib.log('here 1');
12+
var url = 'http://' + serverInfo.ip + ':3004/sse';
13+
lib.log('here 2');
14+
lib.eventStream= new EventSource(url);
15+
lib.log('here 3');
16+
lib.eventStream.onmessage = (e)=> {
17+
lib.log('here 5');
18+
const data = JSON.parse(e.data);
19+
lib.log(data);
20+
};
21+
lib.log('here 4');
22+
} ),
723

824
key : ( function key( k ) {
925
k = k.toString();
@@ -44,6 +60,10 @@ key : ( function key( k ) {
4460
lib.display( k );
4561
break;
4662

63+
case 's':
64+
lib.establishSSE();
65+
break;
66+
4767
// Browser
4868
case 'Escape':
4969
notice.innerHTML = 'Reload';
@@ -96,8 +116,10 @@ dorequest : ( function dorequest( path, reload, callback ) {
96116

97117
display : ( function display(key) {
98118
lib.dorequest('/display?key='+ key, false, (data) => {
119+
let d=JSON.parse(data)
99120
var info = document.getElementById("info");
100-
info.innerHTML = data;
121+
info.innerHTML = d.content;
122+
lib.currentDisplay = d.currentDisplay;
101123
} );
102124
}),
103125

@@ -109,7 +131,7 @@ prev : ( function prev() {
109131
lib.dorequest('/prev?width=' + window.screen.width + '&height=' + window.screen.height, true);
110132
}),
111133

112-
sel0 : ( function sel0(c) {
134+
chooseView : ( function chooseView(c) {
113135
lib.dorequest('/sel?view=' + c + '&width=' + window.screen.width + '&height=' + window.screen.height, true);
114136
}),
115137

@@ -137,18 +159,15 @@ keypress : ( function keypress(key) {
137159
}),
138160

139161
sel : ( function sel(c) {
162+
if ( lib.buttonTimer != '' ) window.clearTimeout( lib.buttonTimer );
163+
lib.buttonTimer = "";
140164
lib.queue.push(c.toString().toUpperCase());
141165
notice.innerHTML=lib.queue.join("")
142-
if (!lib.changeView) {
143-
lib.changeView = true;
144-
window.setTimeout(function () {
145-
var ch = { value: '' };
166+
lib.buttonTimer = window.setTimeout(function () {
146167
var newch = lib.queue.join("");
147-
lib.changeView = false;
148168
lib.queue = [];
149-
lib.sel0(newch);
150-
}, 1000);
151-
}
169+
lib.chooseView(newch);
170+
}, 1000);
152171
}),
153172

154173
fullScreen : ( function fullScreen() {

server.js

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let controls = {};
7676
let zoom = false;
7777
let currentDisplay = '';
7878
let htmlText = {};
79-
let pollVars = {};
79+
let httpVars = {};
8080
let intervals = [];
8181

8282
app.use(cors(corsOptions));
@@ -244,8 +244,8 @@ function processConfig() {
244244
for ( let t of config.text ) {
245245
htmlText[ t.name ] = t.frames;
246246
}
247-
for ( let p of config.poll ) {
248-
pollVars[ p.name ] = p;
247+
for ( let p of config.vars ) {
248+
httpVars[ p.name ] = p;
249249
}
250250
setUpPolling();
251251
}
@@ -438,7 +438,7 @@ function pollVariable( name, url ) {
438438
console.log('There was a problem with the request. [' + xhr.status + ']');
439439
} else {
440440
console.log( name + ' = ' + xhr.responseText );
441-
pollVars[ name ] = xhr.responseText;
441+
httpVars[ name ].val = xhr.responseText;
442442
}
443443
}
444444
};
@@ -454,14 +454,16 @@ function setUpPolling() {
454454
clearInterval( intervals[ i ] );
455455
}
456456
intervals = [];
457-
for ( let p in pollVars ) {
458-
console.log( pollVars[p].name );
459-
console.log( pollVars[p].url );
460-
console.log( pollVars[p].interval );
461-
pollVariable( p, pollVars[p].url );
462-
let url = pollVars[p].url;
463-
inv = setInterval( function() { pollVariable( p, url ) }, pollVars[p].interval * 1000 );
464-
intervals.push(inv);
457+
for ( let p in httpVars ) {
458+
console.log( httpVars[p].name );
459+
console.log( httpVars[p].url );
460+
console.log( httpVars[p].interval );
461+
if ( httpVars[p].url !== undefined ) {
462+
pollVariable( p, httpVars[p].url );
463+
let url = httpVars[p].url;
464+
inv = setInterval( function() { pollVariable( p, url ) }, httpVars[p].interval * 1000 );
465+
intervals.push(inv);
466+
}
465467
}
466468
}
467469

@@ -500,6 +502,54 @@ function get_view_list( ) {
500502
return result;
501503
}
502504

505+
function expand_text( content ) {
506+
if ( streams.length > 0 ) {
507+
for ( let v in streams[ 0 ].options.var_map ) {
508+
re = replre( '{{' + v + '}}' );
509+
content = content.replace( re, streams[ 0 ].options.var_map[ v ] );
510+
}
511+
}
512+
for ( let p in httpVars ) {
513+
re = replre( '{{' + p + '}}' );
514+
content = content.replace( re, httpVars[ p ].val );
515+
}
516+
re = replre( '{{view-list}}' );
517+
content = content.replace( re, get_view_list() );
518+
return content;
519+
}
520+
521+
app.get('/set', async (req, res, next) => {
522+
if ( req.query.var !== undefined && req.query.val !== undefined ) {
523+
if ( streams.length > 0 && req.query.var in streams[ 0 ].options.var_map ) {
524+
// this likely isn't extremely useful, as var_map gets regenerated on reload
525+
streams[ 0 ].options.var_map[ req.query.var ] = req.query.val;
526+
} else {
527+
httpVars[ req.query.var ].val = req.query.val;
528+
}
529+
}
530+
return res.send( "OK" );
531+
});
532+
533+
app.get('/get', async (req, res, next) => {
534+
result = "";
535+
if ( req.query.var !== undefined ) {
536+
if ( streams.length > 0 && req.query.var in streams[ 0 ].options.var_map ) {
537+
result = streams[ 0 ].options.var_map[ req.query.var ];
538+
} else if ( req.query.var in httpVars ) {
539+
result = httpVars[ req.query.var ].val;
540+
}
541+
}
542+
return res.send( JSON.stringify( { result } ) );
543+
});
544+
545+
app.get('/expand', async (req, res, next) => {
546+
result = "";
547+
if ( req.query.text !== undefined ) {
548+
result = expand_text( req.query.text );
549+
}
550+
return res.send( JSON.stringify( { result } ) );
551+
});
552+
503553
app.get('/display', async (req, res, next) => {
504554
var key;
505555
try {
@@ -540,37 +590,28 @@ app.get('/display', async (req, res, next) => {
540590
currentDisplay = key;
541591
}
542592
}
593+
let content;
543594
if ( htmlText[ currentDisplay ] ) {
544595
if ( currentFrame >= htmlText[ currentDisplay ].length ) {
545596
currentFrame = 0;
546597
currentDisplay = '';
547-
return res.send( config.html );
548-
}
549-
if ( htmlText[ currentDisplay ].length ) {
550-
h = htmlText[ currentDisplay ][ currentFrame ] + config.html;
598+
content = config.html;
599+
} else if ( htmlText[ currentDisplay ].length ) {
600+
content = htmlText[ currentDisplay ][ currentFrame ] + config.html;
551601
}
552602
} else
553-
h = config.html;
603+
content = config.html;
554604

555605
let re;
556-
if ( streams.length > 0 ) {
557-
for ( let v in streams[ 0 ].options.var_map ) {
558-
re = replre( '{{' + v + '}}' );
559-
h = h.replace( re, streams[ 0 ].options.var_map[ v ] );
560-
}
561-
}
562-
for ( let p in pollVars ) {
563-
re = replre( '{{' + p + '}}' );
564-
h = h.replace( re, pollVars[ p ] );
565-
}
566-
re = replre( '{{view-list}}' );
567-
h = h.replace( re, get_view_list() );
568-
return res.send( h );
606+
content = expand_text( content );
607+
608+
return res.send( JSON.stringify( { content, currentDisplay } ) );
569609
} catch ( err ) {
570610
console.log( err );
571611
next( err );
572612
}
573-
return res.send('<B><CENTER><FONT SZ=+4>Define content for ' + key + ' in config</FONT></CENTER></B>' + config.html);
613+
content = '<B><CENTER><FONT SZ=+4>Define content for ' + key + ' in config</FONT></CENTER></B>' + config.html;
614+
return res.send( JSON.stringify( { content, currentDisplay } ) );
574615
});
575616

576617
app.get('/keypress', async (req, res) => {
@@ -710,4 +751,41 @@ app.post('/admin/config/save', protect(), async (req, res) => {
710751
}));
711752
});
712753

754+
755+
app.get('/sse', (req, res) => {
756+
console.log('A');
757+
res.writeHead(200, {
758+
'Content-Type': 'text/event-stream',
759+
'Cache-Control': 'no-cache',
760+
'Connection': 'keep-alive'
761+
});
762+
addSSEClient(req, res);
763+
});
764+
765+
let clientData = [];
766+
let sequence = 1;
767+
function addSSEClient(req, res) {
768+
console.log('B');
769+
req.on('close', () => clientData = clientData.filter(o => o.res !== res));
770+
clientData.push({
771+
res,
772+
clientNo: sequence++,
773+
messageNo: 1
774+
});
775+
}
776+
function send() {
777+
clientData.forEach(o => {
778+
console.log('C');
779+
const data = JSON.stringify({
780+
clientCount: clientData.length,
781+
clientNo: o.clientNo,
782+
messageNo: o.messageNo++
783+
});
784+
o.res.write(`data: ${data}\n\n`);
785+
});
786+
setTimeout(send, 1000);
787+
}
788+
789+
setTimeout(send);
790+
713791
app.listen(3004);

0 commit comments

Comments
 (0)