]> Untitled Git - go.git/blob - socket/server.js
Switched to nginx alpine
[go.git] / socket / server.js
1 var express = require('express');
2 var app = express();
3 var expressWs = require('express-ws')(app);
4
5 var db = require('./db');
6
7 function sleep(ms) {
8   return new Promise(resolve => setTimeout(resolve, ms));
9 }
10
11 async function pollStatefulChange(ws, session_id) {
12     var lastUpdated  = 0;
13     var rowCount = 0;
14     while (true) {
15         try {
16             var res = await db.getMaxUpdatedState(session_id);
17             var newRowCount = res[0].num_rows;
18             var updatedAt = res[0].last_updated;
19
20             // update board state of client if more moves
21             // have been added since max last timestamp
22             // If more moves have been added in <1 sec,
23             // use the row count for the max last updated timestamp
24             if (updatedAt > lastUpdated || rowCount < newRowCount) {
25                 lastUpdated = updatedAt;
26                 rowCount = newRowCount;
27                 ws.send(JSON.stringify({
28                     "type": "board",
29                     "data": await db.getBoardState(session_id)
30                 }));
31             }
32         } catch(err) {
33             console.log(`websocket poll error: ${err}`);
34         }
35         await sleep(1000);
36     }
37 }
38
39 app.ws('/ws', async function(ws, req) {
40     // poll for stateful change and notify clients to update their boards
41     var session_id = 0;
42     db.initBoard();
43
44     // get session ID
45     ws.send(JSON.stringify({
46         "type": "session"
47     }));
48
49     ws.on('message', async function(msg) {
50         let parsed;
51         try {
52             parsed = JSON.parse(msg);
53             switch (parsed.type) {
54                 case "session":
55                     session_id = parsed.data.session
56
57                     // send initial message to draw session client board
58                     ws.send(JSON.stringify({
59                         "type": "board",
60                         "data": await db.getBoardState(session_id)
61                     }));
62                     break;
63                 case "new":
64                     ws.send(JSON.stringify({
65                         "type": "new",
66                         "data": await db.deleteSession(parsed.data.session)
67                     }));
68                     break;
69                 case "move":
70                     await db.addMove(
71                         parsed.data.session,
72                         parsed.data.x,
73                         parsed.data.y,
74                         parsed.data.state,
75                         );
76                     // fall through and return new board state
77                 case "board":
78                     ws.send(JSON.stringify({
79                         "type": "board",
80                         "data": await db.getBoardState(parsed.data.session)
81                     }));
82                     break;
83                 default:
84                     console.log("ws message: Unknown message type: " + type);
85             }
86         } catch(err) {
87             console.log(`ws message error: ${err}`);
88         }
89     });
90
91     pollStatefulChange(ws, session_id);
92 });
93
94 app.listen(3000);