-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (55 loc) · 1.56 KB
/
server.js
File metadata and controls
64 lines (55 loc) · 1.56 KB
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
require('dotenv').config()
const { join } = require('path');
const { Server } = require("socket.io");
const http = require('http');
const express = require('express')
const { createClient } = require('@supabase/supabase-js');
const app = express()
const server = http.createServer(app);
const io = new Server(server);
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY,
);
app.get('/', (req, res) => {
res.sendFile(join(__dirname, '/index.html'));
})
app.get('/test', (req, res) => {
res.sendFile(join(__dirname, '/test.html'));
})
app.get('/debug', (req, res) => {
res.sendFile(join(__dirname, '/debug.html'));
})
app.use(express.static('.'))
server.listen(3000, () => {
console.log('listening on *:3000');
server.emit('reload')
});
let id = Math.random();
io.on('connection', function (socket) {
io.emit('reload', id)
socket.on('debug', (args) => {
io.emit('debug', args)
});
socket.on('debugX', (args) => {
io.emit('debugX', args)
});
socket.on('commitShape', async ({ color, creationFrameCount, lastSeenFrame, coords }, width, height, earthTones) => {
const rgb = earthTones.find(({ color: name }) => name === color).rgb
const { data, error } = await supabase
.from('postparty_shapes')
.insert(
[
{
color: rgb,
start: creationFrameCount,
end: lastSeenFrame,
coords: JSON.stringify(coords.map(({ x, y }) => ({ x: x / width, y: y / height }))),
}
]
);
if (error) {
console.log(error);
}
});
});