diff --git a/Source/Client.js b/Source/Client.js index dcf38fd..72b061b 100644 --- a/Source/Client.js +++ b/Source/Client.js @@ -1,3 +1,5 @@ +const { CLIENT_SEND_MESSAGE } = require("./Notify"); + function Client() { /** @@ -21,6 +23,9 @@ function Client() this.APNumber = 0; this.APShortCode = 0; this.APIPAddress = 0; + + this.isProxy = false; + this.proxyProcess = null; }; /** * @type {Map} @@ -75,7 +80,12 @@ Client.prototype.pairList = function(){ }; Client.prototype.send = function(obj){ - this.socket.sendUTF(JSON.stringify(obj)); + if(this.isProxy) + { + CLIENT_SEND_MESSAGE(this.id, obj) + }else{ + this.socket.sendUTF(JSON.stringify(obj)); + } }; exports.Client = Client; \ No newline at end of file diff --git a/Source/HTTPServer.js b/Source/HTTPServer.js index 47bc01b..af9fb31 100644 --- a/Source/HTTPServer.js +++ b/Source/HTTPServer.js @@ -11,11 +11,19 @@ server.addListener("request", app); app.use(compression({ level: 9 })); + server.listen(7707,'0.0.0.0',() => { termoutput && console.log("HTTP Service Running..."); }); +server.addListener("error",(err)=> { + console.err(err) +}) exports.http = server; +app.get("/",(req,res)=>{ + res.send("pid : " + process.pid) +}) + app.get("/script",(request, response)=>{ response.sendFile(resolve("./script/index.js")) }); diff --git a/Source/Notify.js b/Source/Notify.js new file mode 100644 index 0000000..9b34a38 --- /dev/null +++ b/Source/Notify.js @@ -0,0 +1,66 @@ +process.on('message',data => { + const {Client} = require("./Client.js") + switch(data.type) + { + case "CLIENT_CREATED":{ + let client = new Client(); + client.isProxy = true; + client.proxyProcess = data.pid; + client.id = data.uuid; + Client.clients.set(client.id, client); + break; + } + case "CLIENT_UPDATE_PROP":{ + let client = Client.clients.get(data.uuid); + client[data.name] = value; + break; + } + case "CLIENT_SEND_MESSAGE":{ + let client = Client.clients.get(data.uuid); + client.send(data.message) + break; + } + case "CLIENT_DESTROY":{ + Client.clients.delete(data.uuid); + break; + } + } +}); + +function CLIENT_CREATED(uuid) +{ + console.log(process.pid, "CLIENT_CREATED"); + process.send({ + type:'CLIENT_CREATED', + uuid: uuid + }) +}; +function CLIENT_UPDATE_PROP(uuid, name, value) +{ + process.send({ + type:'CLIENT_UPDATE_PROP', + uuid: uuid, + name, + value + }) +}; +function CLIENT_SEND_MESSAGE(uuid, message) +{ + process.send({ + type:'CLIENT_SEND_MESSAGE', + uuid: uuid, + message + }) +}; +function CLIENT_DESTROY(uuid) +{ + process.send({ + type:'CLIENT_DESTROY', + uuid: uuid + }) +}; + +exports.CLIENT_CREATED = CLIENT_CREATED; +exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP; +exports.CLIENT_DESTROY = CLIENT_DESTROY; +exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE; \ No newline at end of file diff --git a/Source/WebSocket.js b/Source/WebSocket.js index 470e41e..c972c86 100644 --- a/Source/WebSocket.js +++ b/Source/WebSocket.js @@ -5,6 +5,7 @@ let {http} = require("./HTTPServer"); let {randomUUID} = require("crypto"); const { Client } = require("./Client.js"); const { termoutput } = require("./config"); +const { CLIENT_CREATED, CLIENT_DESTROY } = require("./Notify"); termoutput && console.log("Web Socket Protocol is ready"); http.addListener("upgrade",() => { @@ -20,6 +21,9 @@ let global = new Map(); let clients = new Map(); wsServer.addListener("connect",(socket) => { + socket.send("worker id " + process.pid); + return; + let xClient = new Client(); let id = randomUUID(); socket.id = id; @@ -29,9 +33,12 @@ wsServer.addListener("connect",(socket) => { Client.clients.set(id, xClient); clients.set(id, xClient); + CLIENT_CREATED(id); + emit("connect", global, xClient); socket.addListener("close",()=>{ emit("disconnect", global, xClient); + CLIENT_DESTROY(id); Client.clients.set(id, xClient); }); socket.addListener("message",({type,utf8Data}) => { diff --git a/index.js b/index.js new file mode 100644 index 0000000..231b10b --- /dev/null +++ b/index.js @@ -0,0 +1,66 @@ +/** @type {import('node:cluster').Cluster} */ +const cluster = require("cluster"); +const os = require("os"); +const {randomInt} = require("crypto"); + +cluster.schedulingPolicy = cluster.SCHED_RR; + +async function main() +{ + let master = new Map(); + + if(cluster.isPrimary) + { + let e = 0|0 + while (e < 10) + { + e++; + let flow = await generateFlow(); + flow.send({ + TYPE:"start" + }) + master.set(flow.id, flow); + flow.message((_id, obj) =>{ + for (const [id,{send}] of master) { + if(_id !== id) + { + send({ + ...obj, + pid: flow.id + }) + } + } + }) + }; + }else{ + let Application; + let synced = []; + process.on("message",data => { + switch(data.TYPE) + { + case "start":{ + Application = require("./Source/index"); + break; + } + } + }) + } +} + +async function generateFlow(N) +{ + const worker = cluster.fork(); + await new Promise(ok => { + worker.addListener("online",()=> { + ok() + }) + }); + let id = worker.process.pid; + + return { + id, + send: message => worker.send(message), + message: (callback) => worker.addListener("message", e => callback(id,e)) + } +} +process.nextTick(main); \ No newline at end of file diff --git a/test.html b/test.html index d0ecd7a..46e430d 100644 --- a/test.html +++ b/test.html @@ -7,11 +7,11 @@ Document +

\ No newline at end of file