WIP, tout est en chantier, très content :)

This commit is contained in:
LuckyLaszlo
2022-11-20 15:46:45 +01:00
parent 023b5ed485
commit 48665cfe30
22 changed files with 580 additions and 302 deletions

View File

@@ -3,25 +3,13 @@ import http from "http";
import url from "url";
import fs from "fs";
import path from "path";
import { WebSocketServer, WebSocket } from "ws";
import { v4 as uuidv4 } from 'uuid';
import { random } from "../shared_js/utils.js";
import {EventTypes, EventData, EventGameUpdate} from "../shared_js/class/Event.js"
import {Rectangle, MovingRectangle, Player, Ball, Line} from "../shared_js/class/Rectangle.js";
import { Vector } from "../shared_js/class/Vector.js";
import {wsServer} from "./wsServer.js"; wsServer; // no-op
const hostname = "localhost";
const port = 8080;
const wsPort = 8042; // pas indispensable d'avoir un autre port si le WebSocket est limité à certaines routes
const root = "../../www/";
class CanvasRenderingContext2D {} // Empty object replacement to the web-API (web-API useless on server-side)
const mockCtx = new CanvasRenderingContext2D;
// @ts-ignore
const player1 = new Player(mockCtx, new Vector(), "white", 1, 1, 1);
const server = http.createServer((req, res) => {
// var q = new URL(req.url, `http://${req.getHeaders().host}`)
// @ts-ignore
@@ -46,84 +34,3 @@ const server = http.createServer((req, res) => {
server.listen(port, hostname, () => {
console.log(`Pong running at http://${hostname}:${port}/pong.html`);
});
const wsServer = new WebSocketServer({port: wsPort, path: "/pong"});
class Client {
socket: WebSocket;
id: string;
isAlive: boolean = true;
constructor(socket: WebSocket, id: string) {
this.socket = socket;
this.id = id;
}
}
const clientsArr: Client[] = [];
wsServer.on("connection", (socket, request) => {
const id = uuidv4();
const client = new Client(socket, id);
clientsArr.push(client);
socket.on("pong", function heartbeat() {
client.isAlive = true;
console.log("client %s alive at %i", client.id, Date.now());
});
socket.on("message", function log(data) {
console.log("received: %s", data);
});
socket.on("message", clientInputListener);
socket.send(JSON.stringify({type: EventTypes.start}));
// socket.send("connection success, bravo client " + id);
// socket.send("start");
// socket.send("json/20");
});
function clientInputListener(data: string)
{
return;
}
function deleteClient(client: Client)
{
var i = clientsArr.indexOf(client);
if (i != -1) {
clientsArr.splice(i, 1);
}
}
const pingInterval = setInterval( () => {
clientsArr.forEach( (client) => {
if (client.isAlive === false) {
client.socket.terminate();
console.log("client %s is no more at %i :'(", client.id, Date.now());
deleteClient(client);
return;
}
client.isAlive = false;
client.socket.ping();
});
}, 5000);
const gameUpdateInterval = setInterval( () => {
clientsArr.forEach( (client) => {
const update = {
type: EventTypes.gameUpdate,
player1: {y: random(50, 650)},
player2: {y: random(50, 650)},
ball: {x: 0, y: 0, speed: 0}
};
client.socket.send(JSON.stringify(update));
});
}, 500);
wsServer.on('close', () => {
clearInterval(pingInterval);
clearInterval(gameUpdateInterval);
});