refactoring
+ wip input to server
This commit is contained in:
@@ -5,13 +5,23 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { WebSocketServer, WebSocket } from "ws";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { random } from "../client/utils.js";
|
||||
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";
|
||||
|
||||
|
||||
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
|
||||
@@ -63,16 +73,22 @@ wsServer.on("connection", (socket, request) => {
|
||||
console.log("client %s alive at %i", client.id, Date.now());
|
||||
});
|
||||
|
||||
socket.on("message", function message(data) {
|
||||
socket.on("message", function log(data) {
|
||||
console.log("received: %s", data);
|
||||
});
|
||||
|
||||
socket.send(JSON.stringify({type: 2})); // start
|
||||
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);
|
||||
@@ -97,7 +113,7 @@ const pingInterval = setInterval( () => {
|
||||
const gameUpdateInterval = setInterval( () => {
|
||||
clientsArr.forEach( (client) => {
|
||||
const update = {
|
||||
type: 1,
|
||||
type: EventTypes.gameUpdate,
|
||||
player1: {y: random(50, 650)},
|
||||
player2: {y: random(50, 650)},
|
||||
ball: {x: 0, y: 0, speed: 0}
|
||||
|
||||
Reference in New Issue
Block a user