Commit
This commit is contained in:
parent
edfb9e09d1
commit
715ab15a23
|
@ -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<string, Client>}
|
||||
|
@ -75,7 +80,12 @@ Client.prototype.pairList = function(){
|
|||
};
|
||||
|
||||
Client.prototype.send = function(obj){
|
||||
if(this.isProxy)
|
||||
{
|
||||
CLIENT_SEND_MESSAGE(this.id, obj)
|
||||
}else{
|
||||
this.socket.sendUTF(JSON.stringify(obj));
|
||||
}
|
||||
};
|
||||
|
||||
exports.Client = Client;
|
|
@ -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"))
|
||||
});
|
||||
|
|
|
@ -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;
|
|
@ -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}) => {
|
||||
|
|
|
@ -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);
|
22
test.html
22
test.html
|
@ -7,11 +7,11 @@
|
|||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1 id="message"></h1>
|
||||
<script src="./script/index.js"></script>
|
||||
<script>
|
||||
var wsjs, iroom;
|
||||
async function main(){
|
||||
wsjs = new MWSE({
|
||||
const wsjs = new MWSE({
|
||||
endpoint: "ws://localhost:7707"
|
||||
});
|
||||
wsjs.scope(async ()=>{
|
||||
|
@ -33,6 +33,15 @@
|
|||
console.log(args)
|
||||
});
|
||||
iroom = room;
|
||||
/*setInterval(()=>{
|
||||
wsjs.server.tranferToServer({
|
||||
e0:Math.random(),
|
||||
e1:Math.random(),
|
||||
e2:Math.random(),
|
||||
e3:Math.random(),
|
||||
e4:Math.random()
|
||||
})
|
||||
}, 10)*/
|
||||
});
|
||||
wsjs.on('peer',(peer)=>{
|
||||
peer.on('message',(...args)=>{
|
||||
|
@ -40,7 +49,16 @@
|
|||
})
|
||||
})
|
||||
};
|
||||
|
||||
let ws = new WebSocket("ws://localhost:7707");
|
||||
ws.onmessage = function({data}){
|
||||
message.innerText = data;
|
||||
}
|
||||
|
||||
// main();
|
||||
/*for (const iterator of Array.from({length:90}).fill(0)) {
|
||||
main();
|
||||
}*/
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue