-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
74 lines (59 loc) · 1.67 KB
/
index.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
var express = require('express');
const { WebSocketServer } = require('ws');
var app = express();
app.use(express.json());
app.get('/', function(req, res, next){
console.log("hello");
res.send("hello, welcome to events\n");
});
app.get('/yo', function(req, res, next){
console.log("wassup hi");
res.send("wassup hi\n");
});
app.get('/source', async function(req, res) {
console.log('Got /source');
// res.send("hi from /source");
res.set({
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive'
});
res.flushHeaders();
// Tell the client to retry every 10 seconds if connectivity is lost
res.write('retry: 10000\n\n');
let count = 0;
/*
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Emit', ++count);
// Emit an SSE that contains the current 'count' as a string
res.write(`data: ${count}\n\n`);
}*/
});
let lastResults = [];
app.post('/result', async function (req, res) {
const currentResults = req.body;
console.log({currentResults, lastResults});
let matchCount = 0.0;
for (let i = 0; i < currentResults.length; ++i) {
if (lastResults.includes(currentResults[i])) {
++matchCount;
}
}
console.log(matchCount);
lastResults = currentResults;
res.send(`${matchCount / currentResults.length}`);
});
app.listen(3501);
const wss = new WebSocketServer({ port: 3500 });
wss.on('connection', function connection(ws) {
console.log("ws", performance.now());
ws.on('message', function incoming(message) {
try {
// console.log(message.toString());
ws.send(message);
} catch (e) {
console.log(e, message);
}
});
});