1 var express = require('express');
3 var expressWs = require('express-ws')(app);
5 var db = require('./db');
8 return new Promise(resolve => setTimeout(resolve, ms));
11 async function pollStatefulChange(ws, session_id) {
14 conn = await db.pool.getConnection();
17 var res = await conn.query(`
18 select UNIX_TIMESTAMP(MAX(updated_at)) as last_updated
23 let updatedAt = res[0].last_updated;
24 if ( updatedAt > lastUpdated) {
25 console.log("websocket poll: board updated!");
26 lastUpdated = updatedAt;
27 res = await db.getBoardState(session_id);
28 ws.send(JSON.stringify({
36 console.log(`websocket poll error: ${err}`);
42 app.ws('/ws', async function(ws, req) {
43 // poll for stateful change and notify clients to update their boards
45 pollStatefulChange(ws, 0);
47 ws.on('message', async function(msg) {
48 console.log(`ws message: ${msg}`);
50 var parsed = JSON.parse(msg);
51 switch (parsed.type) {
59 // fall through and return new board state
61 var res = await db.getBoardState(
64 ws.send(JSON.stringify({
70 console.log("ws message: Unknown message type: " + type);