try catch
This commit is contained in:
parent
bd0eb1eb66
commit
c2b484b88d
|
@ -0,0 +1,23 @@
|
|||
const fs = require('fs')
|
||||
|
||||
module.exports = {
|
||||
config: {},
|
||||
initConfig: (config_path) => {
|
||||
if (!fs.existsSync(config_path)) {
|
||||
this.config = {
|
||||
"listenPort": 8080,
|
||||
"boardWidth": 1920,
|
||||
"boardHeight": 1080,
|
||||
"serverAddress": "example.com",
|
||||
"saveFile": "./data//board.png",
|
||||
"timeBetweenPixels": 5,
|
||||
"autoSaveTime": 120
|
||||
}
|
||||
fs.writeFileSync(config_path, JSON.stringify(this.config));
|
||||
} else {
|
||||
this.config = JSON.parse(fs.readFileSync(config_path));
|
||||
}
|
||||
|
||||
console.log(this.config);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
// const pg = require('pg');
|
||||
// const { Client } = pg;
|
||||
|
||||
// const client = Client();
|
||||
|
||||
// const init_database = async () => {
|
||||
// await client.connect();
|
||||
// await client.query('CREATE TABLE IF NOT EXIST users');
|
||||
// }
|
82
src/index.js
82
src/index.js
|
@ -104,50 +104,52 @@ server.on('connection', (client) => {
|
|||
console.log(`New client has connected, assigning id ${client.id} to it.`)
|
||||
|
||||
client.on('message', function (msg) {
|
||||
let packet, content, code;
|
||||
try {
|
||||
packet = JSON.parse(msg.toString());
|
||||
content = packet.content;
|
||||
code = packet.code;
|
||||
} catch (e) { console.log(e) }
|
||||
let response = {};
|
||||
switch (code) {
|
||||
case 0:
|
||||
response.code = 0;
|
||||
response.content = board;
|
||||
let packet, content, code;
|
||||
try {
|
||||
packet = JSON.parse(msg.toString());
|
||||
content = packet.content;
|
||||
code = packet.code;
|
||||
} catch (e) { console.log(e) }
|
||||
let response = {};
|
||||
switch (code) {
|
||||
case 0:
|
||||
response.code = 0;
|
||||
response.content = board;
|
||||
|
||||
response.timeBetweenPixels = timeBetweenPixels;
|
||||
client.send(Buffer.from(JSON.stringify(response)));
|
||||
response = {}
|
||||
boradcastOnline();
|
||||
break;
|
||||
case 1:
|
||||
response.code = 1;
|
||||
response.content = content;
|
||||
response.pixelOwner = false;
|
||||
contentJson = JSON.parse(content);
|
||||
let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4);
|
||||
if (contentJson.x < 0 || contentJson.y < 0) client.send(JSON.stringify("{code:-1}"));
|
||||
response.timeBetweenPixels = timeBetweenPixels;
|
||||
client.send(Buffer.from(JSON.stringify(response)));
|
||||
response = {}
|
||||
boradcastOnline();
|
||||
break;
|
||||
case 1:
|
||||
response.code = 1;
|
||||
response.content = content;
|
||||
response.pixelOwner = false;
|
||||
contentJson = JSON.parse(content);
|
||||
let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4);
|
||||
if (contentJson.x < 0 || contentJson.y < 0) client.send(JSON.stringify("{code:-1}"));
|
||||
|
||||
let unixTime = Math.floor(new Date().getTime() / 1000);
|
||||
if (lastPixelTimestamp[client.id] == undefined || unixTime - lastPixelTimestamp[client.id] > timeBetweenPixels - 1) {
|
||||
lastPixelTimestamp[client.id] = unixTime;
|
||||
clients.forEach((c) => {
|
||||
response.pixelOwner = c.id == client.id? true : false;
|
||||
c.send(Buffer.from(JSON.stringify(response)));
|
||||
board[pixelNumber + 0] = contentJson.r;
|
||||
board[pixelNumber + 1] = contentJson.g;
|
||||
board[pixelNumber + 2] = contentJson.b;
|
||||
board[pixelNumber + 3] = 255;
|
||||
})
|
||||
}
|
||||
let unixTime = Math.floor(new Date().getTime() / 1000);
|
||||
if (lastPixelTimestamp[client.id] == undefined || unixTime - lastPixelTimestamp[client.id] > timeBetweenPixels - 1) {
|
||||
lastPixelTimestamp[client.id] = unixTime;
|
||||
clients.forEach((c) => {
|
||||
response.pixelOwner = c.id == client.id? true : false;
|
||||
c.send(Buffer.from(JSON.stringify(response)));
|
||||
board[pixelNumber + 0] = contentJson.r;
|
||||
board[pixelNumber + 1] = contentJson.g;
|
||||
board[pixelNumber + 2] = contentJson.b;
|
||||
board[pixelNumber + 3] = 255;
|
||||
})
|
||||
}
|
||||
break;
|
||||
case undefined:
|
||||
break;
|
||||
case undefined:
|
||||
break;
|
||||
default:
|
||||
console.log("Packet cannot be understood: ", packet);
|
||||
client.send("{\"code\":-1}");
|
||||
}
|
||||
default:
|
||||
console.log("Packet cannot be understood: ", packet);
|
||||
client.send("{\"code\":-1}");
|
||||
}
|
||||
}catch (e) { console.log(e) }
|
||||
});
|
||||
|
||||
client.on('close', function () {
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
module.exports = function() {
|
||||
this.start = async () => {
|
||||
if (!fs.existsSync(config_path)) {
|
||||
config = {
|
||||
"listenPort": 8080,
|
||||
"boardWidth": 1920,
|
||||
"boardHeight": 1080,
|
||||
"serverAddress": "example.com",
|
||||
"saveFile": "./data//board.png",
|
||||
"timeBetweenPixels": 5,
|
||||
"autoSaveTime": 120
|
||||
}
|
||||
fs.writeFileSync(config_path, JSON.stringify(config));
|
||||
} else {
|
||||
config = JSON.parse(fs.readFileSync(config_path));
|
||||
}
|
||||
|
||||
if (!fs.existsSync(saveFile)) {
|
||||
console.log("No save file found, creating blank board.");
|
||||
board.fill(255);
|
||||
|
||||
} else {
|
||||
console.log("Save file found, loading")
|
||||
let image = Jimp.read(`${saveFile}`, (err, image) => {
|
||||
for (let x = 0; x < boardWidth; x++) {
|
||||
for (let y = 0; y < boardHeight; y++) {
|
||||
pixelNumber = evaulatePixelNumber(x * 4, y * 4);
|
||||
let pixel = Jimp.intToRGBA(image.getPixelColor(x, y))
|
||||
board[pixelNumber + 0] = pixel.r
|
||||
board[pixelNumber + 1] = pixel.g
|
||||
board[pixelNumber + 2] = pixel.b
|
||||
board[pixelNumber + 3] = 255;
|
||||
}
|
||||
}
|
||||
console.log("Loaded")
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
this.save = async (err, image) => {
|
||||
if (err) throw err;
|
||||
console.log()
|
||||
for (let x = 0; x < boardWidth; x++) {
|
||||
for (let y = 0; y < boardHeight; y++) {
|
||||
let pixelNumber = evaulatePixelNumber(x * 4, y * 4);
|
||||
image.setPixelColor(
|
||||
Jimp.rgbaToInt(
|
||||
Number(board[pixelNumber + 0]),
|
||||
Number(board[pixelNumber + 1]),
|
||||
Number(board[pixelNumber + 2]),
|
||||
Number(board[pixelNumber + 3])
|
||||
),
|
||||
x, y
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
image.write(`${saveFile}`, (err) => {
|
||||
if (err) throw err;
|
||||
console.log("Saved")
|
||||
if (toQuit) process.exit();
|
||||
});
|
||||
};
|
||||
this.save_notify = (signal) => {
|
||||
console.log(`Caught ${signal}, saving ${boardWidth}x${boardHeight} image`)
|
||||
|
||||
toQuit = signal == 'SIGUSR1' || signal == 'AUTOSAVE' ? false : true;
|
||||
let image = new Jimp(boardWidth, boardHeight, save);
|
||||
};
|
||||
this.evaulatePixelNumber = (x, y) => {
|
||||
let pixelNumber;
|
||||
if (y > 0)
|
||||
pixelNumber = (y) * boardWidth + x;
|
||||
if (y == 0)
|
||||
pixelNumber = x;
|
||||
return pixelNumber;
|
||||
};
|
||||
this.boradcastOnline = () => {
|
||||
let response = {};
|
||||
for (let i = 0; i < clients.length; i++) {
|
||||
response.code = 2;
|
||||
response.online = clients.length;
|
||||
clients[i].send(Buffer.from(JSON.stringify(response)));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue