basic Websocket client-server
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
|
||||
// var http = require("http");
|
||||
// var url = require("url");
|
||||
// var fs = require("fs");
|
||||
// var path = require("path");
|
||||
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 "../client/utils.js";
|
||||
|
||||
const hostname = "localhost";
|
||||
const port = 8080;
|
||||
const root = "/mnt/c/Users/Lucky/Desktop/code/ft_transcendence/www/";
|
||||
const wsPort = 8042; // pas indispensable d'avoir un autre port si le WebSocket est limité à certaines routes
|
||||
const root = "../../www/";
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
// var q = new URL(req.url, `http://${req.getHeaders().host}`)
|
||||
@@ -36,4 +35,79 @@ 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 message(data) {
|
||||
console.log("received: %s", data);
|
||||
});
|
||||
|
||||
socket.send(JSON.stringify({type: 2})); // start
|
||||
// socket.send("connection success, bravo client " + id);
|
||||
// socket.send("start");
|
||||
// socket.send("json/20");
|
||||
});
|
||||
|
||||
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: 1,
|
||||
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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user