-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (160 loc) · 5.23 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
var process = require('process')
const height = 200;
const width = 80;
let fields = new Array(height * width).fill('');
let colors = new Array(height * width).fill('black');
let bgcolors = new Array(height * width).fill('white');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
let clients = new Set();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/index.js', (req, res) => {
res.sendFile(__dirname + '/index.js');
});
app.get('/mvm.ttf', (req, res) => {
res.sendFile(__dirname + '/mvm.ttf');
});
function updateClients() {
const activeClients = JSON.stringify({
action: 'activeClients',
number: Array.from(clients).length,
});
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(activeClients);
}
});
}
wss.on('connection', (ws) => {
console.log('New WebSocket connection established.');
clients.add(ws);
updateClients();
ws.send(JSON.stringify({ action: 'full', fields, colors, bgcolors }));
ws.on('message', (message) => {
let data;
try {
data = JSON.parse(message);
} catch (error) {
ws.send(JSON.stringify({ error: 'Invalid JSON' }));
return;
}
if (data.action === 'full') {
console.log("full requested >...<");
ws.send(JSON.stringify({ action: 'full', fields, colors, bgcolors }));
} else if (data.action === 'fullxy') {
const rows = []
for (let r = 0; r < height; r++) {
const row = []
for (let c = 0; c < width; c++) {
row.push(fields[(r * width) + c]);
}
rows.push(row);
}
ws.send(JSON.stringify({ action: 'fullxy', rows }))
} else if (data.action === 'set' && data.index !== undefined && data.char !== undefined) {
if (typeof data.index === 'number' && typeof data.char === 'string') {
if (data.index > fields.length) {
return;
}
const c = data.char
const char_dec = c;
const length = [...new Intl.Segmenter().segment(char_dec)].length
if (!(length <= 1)) {
return;
}
fields[data.index] = char_dec;
const y = Math.floor(data.index / width);
const x = data.index - (width * y);
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ action: 'set', index: data.index, x: x, y: y, char: char_dec }));
}
});
} else {
ws.send(JSON.stringify({ error: 'Invalid data' }));
}
} else if (data.action === 'setxy' && data.x !== undefined && data.y !== undefined && data.char !== undefined) {
if (typeof data.x === 'number' && typeof data.y === 'number' && typeof data.char === 'string') {
if ((data.x < 0 || data.x >= width) || (data.y < 0 || data.y >= height)) {
return
}
const idx = (data.y * width) + data.x
const c = data.char
const char_dec = c;
const length = [...new Intl.Segmenter().segment(char_dec)].length
if (!(length <= 1)) {
return;
}
fields[idx] = char_dec;
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ action: 'set', index: idx, x: data.x, y: data.y, char: char_dec }));
}
});
} else {
ws.send(JSON.stringify({ error: 'Invalid data' }));
}
} else if (data.action === 'info') {
ws.send(JSON.stringify({ width: width, height: height }))
} else if (data.action === 'color') {
if (!(data.index && data.value)) {
return
}
if (!(typeof data.index === 'number' && typeof data.value === 'string')) {
return
}
if (data.index > fields.length) {
return;
}
colors[data.index] = data.value;
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ action: 'color', index: data.index, value: data.value }));
}
});
} else if (data.action === 'bgcolor') {
if (!(data.index && data.value)) {
return
}
if (!(typeof data.index === 'number' && typeof data.value === 'string')) {
return
}
if (data.index > fields.length) {
return;
}
bgcolors[data.index] = data.value;
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ action: 'bgcolor', index: data.index, value: data.value }));
}
});
} else if (data.action === 'get') {
if (!data.index) {
return;
}
if (!(typeof data.index === 'number')) {
return;
}
ws.send(JSON.stringify({ action: 'get', char: fields[data.index] }));
}
});
ws.on('close', () => {
clients.delete(ws)
updateClients();
console.log('WebSocket connection closed.');
});
});
// poor lil docker container
process.on('SIGINT', () => {
console.info("Interrupted")
process.exit(0)
})
server.listen(8008, () => {
console.log('Server is running on http://localhost:8008');
});