Finish code

This commit is contained in:
Hri7566 2023-10-30 18:03:46 -04:00
parent 03b7c5b515
commit d73fbaec86
5 changed files with 188 additions and 161 deletions

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"arrowParens": "avoid",
"trailingComma": "none",
"tabWidth": 4
}

View File

@ -1,17 +1,17 @@
{
"name": "sync",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chokidar": "^3.5.3",
"dotenv": "^16.3.1",
"ws": "^8.14.2"
}
"name": "sync",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chokidar": "^3.5.3",
"dotenv": "^16.3.1",
"ws": "^8.14.2"
}
}

View File

@ -2,79 +2,90 @@ const WebSocket = require("ws");
const { EventEmitter } = require("events");
class Client extends EventEmitter {
constructor() {
super();
this.bindEventListeners();
}
constructor() {
super();
this.bindEventListeners();
}
start() {
if (!this.started) return;
this.started = true;
start() {
if (!this.started) return;
this.started = true;
this.connect();
}
connect() {
this.ws = new WebSocket(process.env.SYNC_URI);
this.ws.on("open", () => {});
this.ws.addEventListener("close", (evt) => {
this.emit("disconnected");
setTimeout(() => {
this.connect();
}, 1000);
});
this.ws.addEventListener("error", (evt) => {
this.emit("wserror", evt);
this.ws.close();
});
this.ws.addEventListener("message", (evt) => {
try {
const str = evt.data.toString();
const msg = JSON.parse(str);
this.emit(msg.m, msg);
} catch (err) {
this.emit("parse_error", err);
}
});
}
stop() {
this.started = false;
if (this.ws) {
this.ws.close();
delete this.ws;
}
}
say(msg) {
try {
if (this.isConnected()) this.ws.send(JSON.stringify(msg));
return true;
} catch (err) {
console.error(err);
return false;
connect() {
this.ws = new WebSocket(process.env.SYNC_URI);
this.ws.on("open", () => {});
this.ws.addEventListener("close", evt => {
this.emit("disconnected");
setTimeout(() => {
this.connect();
}, 1000);
});
this.ws.addEventListener("error", evt => {
this.emit("wserror", evt);
this.ws.close();
});
this.ws.addEventListener("message", evt => {
try {
const str = evt.data.toString();
const msg = JSON.parse(str);
this.emit(msg.m, msg);
} catch (err) {
this.emit("parse_error", err);
}
});
}
}
log(level, message) {
if (!this.isConnected()) return;
}
stop() {
this.started = false;
isConnected() {
return this.ws.readyState == WebSocket.OPEN;
}
if (this.ws) {
this.ws.close();
delete this.ws;
}
}
bindEventListeners() {
this.on("log", (msg) => {
console.log(`[${msg.level}] ${msg.message}`);
});
}
say(msg) {
try {
if (this.isConnected()) this.ws.send(JSON.stringify(msg));
return true;
} catch (err) {
console.error(err);
return false;
}
}
log(level, message) {
this.say({ m: "log", level, message });
}
broadcast(msg) {
this.say({ m: "broadcast", msg });
}
isConnected() {
return this.ws.readyState == WebSocket.OPEN;
}
bindEventListeners() {
this.on("log", msg => {
console.log(`[Server] [${msg.level}] ${msg.message}`);
});
setInterval(() => {
this.say({
m: "t",
e: Date.now()
});
}, 20000);
}
}
module.exports = { Client };

View File

@ -1,25 +1,25 @@
require("dotenv").config();
// TODO Make this repo a module
let isServer;
process.argv.forEach((val, index, array) => {
if (val == "client") isServer = false;
if (val == "server") isServer = true;
if (val == "client") isServer = false;
if (val == "server") isServer = true;
});
if (typeof isServer == "undefined") {
// Incorrect arguments
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} [client|server]`);
process.exit(1);
// Incorrect arguments
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} [client|server]`);
process.exit(1);
}
if (isServer) {
const { Server } = require("./server");
Server.start();
const { Server } = require("./server");
Server.start();
} else {
const { Client } = require("./client");
const cl = new Client();
cl.connect();
const { Client } = require("./client");
const cl = new Client();
cl.connect();
cl.log("DEBUG", "hello");
}

View File

@ -2,82 +2,93 @@ const WebSocket = require("ws");
const { EventEmitter } = require("events");
function broadcast(msg) {
Server.wss.clients.forEach((ws) => {
ws.say(msg);
});
Server.wss.clients.forEach(ws => {
ws.say(msg);
});
}
function log(level, message) {
console.log(`[${level}] ${message}`);
broadcast({ m: "log", level, message });
console.log(`[${level}] ${message}`);
broadcast({ m: "log", level, message });
}
class Server {
static start() {
this.wss = new WebSocket.Server({
port: process.env.PORT || 10911,
});
this.wss.on("connection", (ws, req) => {
ws.preauth = new EventEmitter();
ws.evt = new EventEmitter();
ws.say = (msg) => {
ws.send(JSON.stringify(msg));
};
log("INFO", "Client connected at " + req.socket.remoteAddress);
ws.on("message", (data, isBinary) => {
try {
if (isBinary) return;
const str = data.toString();
const converted = JSON.parse(str);
if (converted.m) {
ws.preauth.emit(converted.m, converted);
if (ws.authed) {
ws.evt.emit(converted.m, converted);
}
}
} catch (err) {
console.error(err);
}
});
ws.preauth.on("hi", (msg) => {
if (!msg.project) return;
ws.authed = true;
ws.project = msg.project;
});
ws.evt.on("log", (msg) => {
if (!msg.level) return;
if (!msg.message) return;
log(msg.level, `From ${req.socket.remoteAddress}: ${msg.message}`);
});
ws.evt.on("broadcast", (msg) => {
if (!msg.msg) return;
if (typeof msg.msg !== "object") return;
if (!msg.msg.m) return;
broadcast({
m: "broadcast",
msg: msg.msg,
static start() {
this.wss = new WebSocket.Server({
port: process.env.PORT || 10911
});
});
});
}
static stop() {
this.wss.close();
delete this.wss;
}
this.wss.on("connection", (ws, req) => {
ws.preauth = new EventEmitter();
ws.evt = new EventEmitter();
ws.say = msg => {
ws.send(JSON.stringify(msg));
};
log("INFO", "Client connected at " + req.socket.remoteAddress);
ws.on("message", (data, isBinary) => {
try {
if (isBinary) return;
const str = data.toString();
const converted = JSON.parse(str);
if (converted.m) {
ws.preauth.emit(converted.m, converted);
if (ws.authed) {
ws.evt.emit(converted.m, converted);
}
}
} catch (err) {
console.error(err);
}
});
ws.preauth.on("hi", msg => {
if (!msg.project) return;
ws.authed = true;
ws.project = msg.project;
});
ws.evt.on("log", msg => {
if (!msg.level) return;
if (!msg.message) return;
log(
msg.level,
`From ${req.socket.remoteAddress}: ${msg.message}`
);
});
ws.evt.on("broadcast", msg => {
if (!msg.msg) return;
if (typeof msg.msg !== "object") return;
if (!msg.msg.m) return;
broadcast({
m: "broadcast",
msg: msg.msg
});
});
ws.evt.on("t", msg => {
ws.say({
m: "t",
t: Date.now(),
e: msg.e
});
});
});
}
static stop() {
this.wss.close();
delete this.wss;
}
}
module.exports = { Server };