-
Notifications
You must be signed in to change notification settings - Fork 1
/
server2.js
388 lines (314 loc) · 9.42 KB
/
server2.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// TODO:
// =========================
// - resize event
// - give size back
// - help ersonse on faulty input
var blessed = require('blessed');
// Create a screen object.
var screen = blessed.screen({
// smartCSR: true,
// fastCSR: true,
// useBCE: true,
// terminal: 'xterm-256color',
// fullUnicode: true,
title: 'ascii-flut',
style: {
fg: 'black',
bg: 'red'
},
log: './debug.log'
});
function renderLoop() {
screen.render()
// console.log(pixelBuffer)
screen.log(pixelBuffer[0].children.length)
// blackRender()
ticker.tick()
}
setInterval(renderLoop, 1000 / 30)
console.log(screen.cols, screen.rows)
var pixelBuffer = []
function randomRender() {
for (var row = 0; row < screen.rows; row++) {
for (var col = 0; col < screen.cols; col++) {
var color = randomColor()
updatePixel(col, row, color)
}
}
}
function blackRender() {
for (var row = 0; row < screen.rows; row++) {
for (var col = 0; col < screen.cols; col++) {
var r = '00'
var g = '00'
var b = '00'
var color = '#' + String(r) + String(g) + String(b)
updatePixel(col, row, color)
}
}
}
function numbersRender() {
for (var row = 0; row < screen.rows; row++) {
for (var col = 0; col < screen.cols; col++) {
var r = '00'
var g = '00'
var b = '00'
var color = '#' + String(r) + String(g) + String(b)
// console.log(row)
var rowChar = String(row).split('').pop()
var colChar = String(col).split('').pop()
if (!row)
rowChar = colChar
updatePixel(col, row, color, rowChar)
}
}
}
function createPixel(x, y, bgcolor, content, fgcolor) {
var pixel = blessed.element({
parent: screen,
top: y,
left: x,
width: 1,
height: 1,
invisible: false,
transparent: false,
content: content || '',
index:10,
style: {
bold: true,
bg: bgcolor || '#000000',
fg: fgcolor || '#000000'
}
})
setPixelBufferWithPixel(pixel)
}
var hexToRGB = function(hex) {
if (hex.length === 4) {
hex = hex[0]
+ hex[1] + hex[1]
+ hex[2] + hex[2]
+ hex[3] + hex[3];
}
var col = parseInt(hex.substring(1), 16)
, r = (col >> 16) & 0xff
, g = (col >> 8) & 0xff
, b = col & 0xff;
return [r, g, b];
}
function updatePixel(x, y, bgcolor, content, fgcolor) {
var pixel = getPixelBufferWithCoords(x, y)
if (content) {
if (String(content) === 'false') {
content = 'clear'
} else {
content = content && content.toString().substr(0, 1)
}
}
// screen.log(x, y, color, content)
if (!pixel) {
createPixel(x, y, bgcolor, content, fgcolor)
} else {
screen.log(bgcolor)
if (bgcolor) {
// var bgRGB = hexToRGB(bgcolor)
// if (bgRGB) {
// pixel.style.bg = 'rgba('+bgRGB[0]+','+bgRGB[1]+','+bgRGB[2]+',100)'
// }
pixel.style.bg = bgcolor
}
if (fgcolor) pixel.style.fg = fgcolor
if (content) pixel.setContent(content)
if (content === 'clear') pixel.setContent('')
// screen.log(x, y, bgcolor, content, fgcolor, pixel.style)
}
}
function getPixelBufferWithCoords(x, y) {
return pixelBuffer[y * screen.cols + x]
}
function setPixelBufferWithPixel(pixel) {
// pixel as in a blessed.element
pixelBuffer[pixel.top * screen.cols + pixel.left] = pixel
}
function updatePixelWithString(str) {
var params = str.toString().split(' ')
// console.log(params)
if (params && params.length >= 4 && params.length <= 6) {
var f = params[0].toString()
var x = parseInt(params[1])
var y = parseInt(params[2])
if (x >= screen.cols || y >= screen.rows || x < 0 || y < 0) return
if (f === "PX") {
var bgcolor = String(params[3]) ? '#' + String(params[3]) : false
if (params.length === 4) {
updatePixel(x, y, bgcolor, false, false)
} else {
var content = String(params[4]) || false
var fgcolor = String(params[5]) ? '#' + String(params[5]) : false
updatePixel(x, y, bgcolor, content, fgcolor)
}
} else if (f === "TX") {
var content = String(params[3]) || false
var fgcolor = String(params[4]) ? '#' + String(params[4]) : false
updatePixel(x, y, false, content, fgcolor)
} else if (f === "FG") {
var fgcolor = String(params[4]) ? '#' + String(params[4]) : false
updatePixel(x, y, false, false, fgcolor)
}
}
}
// initalize/render black background
blackRender()
var info = blessed.element({
parent: screen,
bottom: 0,
left: 0,
width: '100%',
height: 1,
content: 'asciiflut 0.0.0.0:1234',
index: 5,
style: {
bg: '#272727',
fg: '#262626'
}
})
// tc p server
var net = require('net');
var server = net.createServer(function(socket) {
// socket.end('goodbye\n');
socket.on('error', function(err) {
// handle errors here
// console.log(err)
// throw err;
});
socket.on('data', function(data) {
if (data) {
var packets = data.toString().split('\n')
for (var i = 0; i < packets.length; i++) {
updatePixelWithString(packets[i])
}
server.getConnections(function(err, count) {
if (!err && count) {
info.setContent('asciiflut '+server.address().address+':1234 Connections:' + count)
}
})
}
});
})
server.listen({
port: 1234,
host: '0.0.0.0'
}, function() {
address = server.address();
console.log('opened server on %j', address);
// Render the screen.
screen.render();
info.setContent('asciiflut '+server.address().address+':1234')
});
// Quit on Escape, q, or Control-C.
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
function randomHex() {
var hex = (parseInt(Math.random() * 254)).toString(16)
if (hex.length < 2) {
hex = '0' + hex
}
return hex
}
function randomColor() {
var r = randomHex()
var g = randomHex()
var b = randomHex()
return '#' + r + g + b
}
var fps = require('fps')
var ticker = fps({
every: 10 // update every 10 frames
})
var fpsInfo = blessed.element({
parent: screen,
right: 0,
bottom: 0,
width: 3,
height: 1,
content: 'FPS',
style: {
bg: '#666666',
fg: '#ffffff'
}
})
ticker.on('data', function(framerate) {
// console.log('dataaa',framerate)
fpsInfo.setContent(String(parseInt(framerate)))
// screen.render()
})
var http = require('http'),
path = require('path'),
url = require('url'),
Canvas = require('canvas'),
fs = require('fs'),
stream = require('stream')
var passThrough = new stream.PassThrough()
renderRate = 1000 / 0.5
renderInterval = null
scaleFactor = 10
cw = 64
ch = 32
canvas = new Canvas(cw*scaleFactor, ch*scaleFactor)
ctx = canvas.getContext('2d')
var config = {
port: 1235
}
// style: { bg: '#007256', fg: '#000000' },
// content: '',
function renderedImage() {
ctx.save();
ctx.translate(0, 0);
for (var row = 0; row < ch; row++){
for (var col = 0; col < cw; col++){
var pixel = getPixelBufferWithCoords(col,row)
var bg = pixel.style.bg
var fg = pixel.style.fg
var content = pixel.content || ''
ctx.fillStyle = bg;
ctx.fillRect( col*scaleFactor, row*scaleFactor, 1*scaleFactor, 1*scaleFactor )
// ctx.strokeStyle = fg;
ctx.font = "10pt verdana";
ctx.fillStyle = fg;
ctx.fillText(content, col*scaleFactor, row*scaleFactor);
}
}
ctx.restore()
return canvas.toBuffer()
}
var maxFPS = 30
var lastFile = null
var lastFileLength = 0
var boundary = 'harkharkharkharkharkharkharkharkharkharkharkhark';
http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
if (uri == '/img.mjpeg') {
res.useChunkedEncodingByDefault = false;
res.writeHead(200, { 'Content-type': 'multipart/x-mixed-replace; boundary=--' + boundary }, { 'Cache-Control': 'no-cache' }, { 'Connection': 'keep-alive' }, { 'Pragma': 'no-cache' });
var updateLoop = setInterval(function() {
parseBuffer(res, 'img.mjpeg')
}, 1000 / maxFPS);
res.on('close', function() {
clearInterval(updateLoop);
});
} else {
res.writeHead(200, { 'Content-type': 'text/html' });
res.end('<html><body style="width:100%;height:100%;padding:0px;margin:0px;background:black;"><img src="/img.mjpeg" style="width:100%;height:auto;"></body></html>');
}
}).listen(config.port);
var parseBuffer = function(res, imgpath) {
var imgbuffer = renderedImage();
if (imgbuffer.length > 10) {
res.write('Content-Type: image/jpeg\r\nContent-Length: ' + (imgbuffer.length) + '\r\n\r\n');
res.write(imgbuffer);
res.write('\r\n--' + boundary + '\r\n');
} else {
console.log('dropped')
}
}
console.log('listening on port ' + config.port);