Compare commits

..

8 Commits

Author SHA1 Message Date
APEX FIGHT
cf607fa060 Merge branch 'main' of https://gitea.apexfight.net/apex/CSNC 2025-02-24 21:52:36 -05:00
APEX FIGHT
ef87716a8c added timeout to CSNC server 2025-02-24 21:52:30 -05:00
8459e7df21 Update README.md 2025-01-03 05:46:18 +00:00
9785da2e0d Update Client/Unity/README.md 2025-01-03 05:42:03 +00:00
e67060aaf7 Update README.md 2025-01-03 05:41:41 +00:00
a84cb21d8b Update README.md 2025-01-03 05:40:32 +00:00
aa1be3c082 Update README.md 2025-01-03 05:34:50 +00:00
APEX FIGHT
62a1d22528 added tick interval options (to reduce network load from 80tps lololol) 2025-01-03 00:33:48 -05:00
4 changed files with 48 additions and 14 deletions

View File

@@ -6,9 +6,10 @@ using UnityEngine.Networking;
public class CSNC : MonoBehaviour public class CSNC : MonoBehaviour
{ {
public float tickInterval = 5f; // x times per sec
public string ip; public string ip;
public GameObjectManager manager; public GameObjectManager manager;
private float lastPing = 0; //last time connected to server
public class EZtransform //ez consistent serialization of transforms public class EZtransform //ez consistent serialization of transforms
{ {
@@ -79,9 +80,13 @@ public class CSNC : MonoBehaviour
void FixedUpdate() void FixedUpdate()
{ {
if (Time.timeSinceLevelLoad - (1000f / tickInterval) > lastPing)
{
lastPing = Time.timeSinceLevelLoad;
IEnumerator req = request(); IEnumerator req = request();
StartCoroutine(req); StartCoroutine(req);
} }
}
IEnumerator request() IEnumerator request()
{ {
//Debug.Log(GameObjectRegistry.instance.registeredObjects); //Debug.Log(GameObjectRegistry.instance.registeredObjects);

View File

@@ -1,5 +1,5 @@
# Unity Setup # Unity Setup
First, set the ip of the csnc compatible server. First, set the ip of the csnc compatible server. \
(it needs to be formatted as such http://192.51.100.123:1234/ or http://mydomain.tld:1234/) (it needs to be formatted as such http://192.51.100.123:1234/ or http://mydomain.tld:1234/)
![image](./InstructionalImages/ipchange.png) ![image](./InstructionalImages/ipchange.png)

View File

@@ -1,7 +1,16 @@
# CSNC # CSNC
Client-Sync-Net-Code for unity game objects Client-Sync-Net-Code for unity (but potentially not just unity) game objects
A set of very basic drop-in scripts for client-server games that allows 'p2p' style synchronization A set of very basic drop-in scripts for client-server games that allows 'p2p' style synchronization
great for synchronizing simple visual things that are mostly controlled by clients such as particles or thrown objects great for synchronizing simple visual things that are mostly controlled by clients such as particles or thrown objects
## Unity Setup
Please refer to : \
https://gitea.apexfight.net/apex/CSNC/src/branch/main/Client/Unity#unity-setup
## NodeJS Setup
Email me at vlrtch3571@gmail.com if you want me to make a tutorial or need help
this project is about 90% complete other than the godot and go implementations which will be done if i ever decide to make anything with either one

View File

@@ -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 var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
//below is an example of the structure of gameobjectstore //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", "type": "razorblade",
"transform": { "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 {http.ServerResponse<http.IncomingMessage>} res
* @param {string} req * @param {string} req
@@ -53,9 +72,7 @@ async function handleResponse(res, req) {
var request = JSON.parse(req); var request = JSON.parse(req);
let gameObjects = request.gameObjects; 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.writeHead(200);
res.write(JSON.stringify(responseObjectHelper(request.uuid))); 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 //preps and returns data to send back to client
function responseObjectHelper(uuid) { function responseObjectHelper(uuid) {
var responseObject = { var responseObject = {
@@ -76,9 +94,11 @@ function responseObjectHelper(uuid) {
for (let key in gameObjectStore) { for (let key in gameObjectStore) {
if (key == uuid) continue; //dont send back players own data 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; return responseObject;
} }
setInterval(checkTimeout, timeoutTickRate * 1000);
exports.handleResponse = handleResponse; exports.handleResponse = handleResponse;