added timeout to CSNC server
This commit is contained in:
parent
62a1d22528
commit
ef87716a8c
@ -1,9 +1,13 @@
|
||||
const timeoutTickRate = 1; //timeout check every x seconds
|
||||
const timeoutLength = 5000; //timeout length in ms
|
||||
var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
||||
|
||||
//below is an example of the structure of gameobjectstore
|
||||
/**
|
||||
* {
|
||||
* "2c0a48e9-40c6-4139-9697-992397773b12": [
|
||||
* "2c0a48e9-40c6-4139-9697-992397773b12":
|
||||
* time: Date.now(),
|
||||
* gameObjects: [
|
||||
* {
|
||||
"type": "razorblade",
|
||||
"transform": {
|
||||
@ -42,6 +46,21 @@ var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} uuid
|
||||
*/
|
||||
function removePlayer(uuid) {
|
||||
delete gameObjectStore[uuid];
|
||||
}
|
||||
|
||||
function checkTimeout() {
|
||||
for (let key in gameObjectStore) {
|
||||
if (Date.now() - gameObjectStore[key].time > timeoutLength) {
|
||||
removePlayer(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {http.ServerResponse<http.IncomingMessage>} res
|
||||
* @param {string} req
|
||||
@ -53,9 +72,7 @@ async function handleResponse(res, req) {
|
||||
var request = JSON.parse(req);
|
||||
let gameObjects = request.gameObjects;
|
||||
|
||||
gameObjectStore[request.uuid] = gameObjects; //store gameobjects to send to other players
|
||||
|
||||
|
||||
gameObjectStore[request.uuid] = {gameObjects: gameObjects, time: Date.now()}; //store gameobjects to send to other players
|
||||
|
||||
res.writeHead(200);
|
||||
res.write(JSON.stringify(responseObjectHelper(request.uuid)));
|
||||
@ -68,6 +85,7 @@ async function handleResponse(res, req) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//preps and returns data to send back to client
|
||||
function responseObjectHelper(uuid) {
|
||||
var responseObject = {
|
||||
@ -76,9 +94,11 @@ function responseObjectHelper(uuid) {
|
||||
|
||||
for (let key in gameObjectStore) {
|
||||
if (key == uuid) continue; //dont send back players own data
|
||||
responseObject.gameObjects = responseObject.gameObjects.concat(gameObjectStore[key]);
|
||||
responseObject.gameObjects = responseObject.gameObjects.concat(gameObjectStore[key].gameObjects);
|
||||
}
|
||||
|
||||
return responseObject;
|
||||
}
|
||||
|
||||
setInterval(checkTimeout, timeoutTickRate * 1000);
|
||||
exports.handleResponse = handleResponse;
|
Loading…
x
Reference in New Issue
Block a user