🎮 Makes each socket connection control different model
This commit is contained in:
+18
-4
@@ -19,6 +19,15 @@ const randomFloatFromInterval = (min, max) =>
|
|||||||
const radToDeg = (val) => val * (180 / Math.PI);
|
const radToDeg = (val) => val * (180 / Math.PI);
|
||||||
const degToRad = (val) => val * (Math.PI / 180);
|
const degToRad = (val) => val * (Math.PI / 180);
|
||||||
|
|
||||||
|
function createNewClientState() {
|
||||||
|
return {
|
||||||
|
model: JSON.parse(JSON.stringify(model)),
|
||||||
|
settings: JSON.parse(JSON.stringify(settings)),
|
||||||
|
logs: [...logs],
|
||||||
|
subscriptions: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function subscribeClientToCategory(ws, category) {
|
function subscribeClientToCategory(ws, category) {
|
||||||
if (!subscriptions[category]) {
|
if (!subscriptions[category]) {
|
||||||
subscriptions[category] = new Set();
|
subscriptions[category] = new Set();
|
||||||
@@ -183,6 +192,8 @@ const updateAngles = (angles) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
wss.on("connection", (ws) => {
|
wss.on("connection", (ws) => {
|
||||||
|
const clientState = createNewClientState();
|
||||||
|
ws.clientState = clientState;
|
||||||
ws.on("error", console.error);
|
ws.on("error", console.error);
|
||||||
|
|
||||||
ws.on("message", (message) => {
|
ws.on("message", (message) => {
|
||||||
@@ -213,9 +224,12 @@ wss.on("connection", (ws) => {
|
|||||||
break;
|
break;
|
||||||
case "kinematic/angle":
|
case "kinematic/angle":
|
||||||
if (data.angle && data.id) {
|
if (data.angle && data.id) {
|
||||||
model.servos.angles[data.id] = data.angle;
|
ws.clientState.model.servos.angles[data.id] = data.angle;
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({ type: "angles", angles: model.servos.angles })
|
JSON.stringify({
|
||||||
|
type: "angles",
|
||||||
|
angles: ws.clientState.model.servos.angles,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
ws.send(JSON.stringify(updateAngle(data.id, data.angle)));
|
ws.send(JSON.stringify(updateAngle(data.id, data.angle)));
|
||||||
@@ -223,11 +237,11 @@ wss.on("connection", (ws) => {
|
|||||||
break;
|
break;
|
||||||
case "kinematic/angles":
|
case "kinematic/angles":
|
||||||
if (data.angles) {
|
if (data.angles) {
|
||||||
model.servos.angles = data.angles;
|
ws.clientState.model.servos.angles = data.angles;
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "angles",
|
type: "angles",
|
||||||
angles: model.servos.angles,
|
angles: ws.clientState.model.servos.angles,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user