Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,21 @@
{
"name": "kha-server",
"version": "1.0.0",
"description": "",
"main": "kha.js",
"dependencies": {
"express": "^4.13.3",
"express-ws": "^0.2.6"
},
"devDependencies": {
"nodemon": "^1.11.0",
"livereloadx": "^0.3.10"
},
"scripts": {
"autoserve": "nodemon kha.js",
"autoreload": "livereloadx ./",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "khamake",
"license": "Zlib"
}

View File

@ -0,0 +1,71 @@
const port = 6789;
const playersPerSession = 2;
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
if (!fs.existsSync('node_modules')) {
console.log('Please run npm install.')
process.exit(1);
}
const express = require('express');
app = express();
require('express-ws')(app);
if (!fs.existsSync(path.join('..', 'html5'))) {
console.log('Please compile for html5 before running the server.');
process.exit(1);
}
app.use('/', express.static('../html5'));
app.use((err, req, res, next) => {
console.error(err.stack);
});
class Session {
constructor() {
this.clients = [];
this.instance = child_process.fork('./kha.js');
this.instance.on('message', (message) => {
this.clients[message.id].send(Buffer.from(message.data));
});
}
}
let sessions = [];
let pendingSession = new Session();
app.ws('/', (socket, req) => {
console.log('Client connected.');
socket.session = pendingSession;
socket.id = socket.session.clients.length;
socket.session.clients.push(socket);
socket.session.instance.send({message: 'connect', id: socket.id});
socket.on('message', function (message) {
socket.session.instance.send({message: 'message', data: message, id: socket.id});
});
socket.onclose = () => {
console.log('Removing client ' + socket.id + '.');
socket.session.instance.send({message: 'disconnect', id: socket.id});
socket.session.clients.remove(socket);
if (socket.session.clients.length === 0) {
socket.session.instance.kill();
sessions.remove(socket.session);
}
};
if (pendingSession.clients.length === playersPerSession) {
console.log('Starting session.');
sessions.push(pendingSession);
pendingSession = new Session();
}
});
console.log('Starting server at ' + port + '.');
app.listen(port);