Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.js #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ try {
}

avatarHistory = [...shared.avatars];

shared.avatars = [];
let userData = {};

try {
Expand All @@ -125,7 +125,6 @@ wss.on("connection", function (client, request) {
let avatar = undefined;

client.on("error", console.error);

client.on("message", function message(data) {
if (data.toString().substring(0, 1) == "{") {
let msg = JSON.parse(data);
Expand All @@ -142,7 +141,7 @@ wss.on("connection", function (client, request) {
case "login":
// create random username if no username was given
if (!msg.username) msg.username = uuidv4()

console.log(msg)
handleLogin(
msg,
Expand All @@ -161,9 +160,22 @@ wss.on("connection", function (client, request) {
}
});

const interval = setInterval(() => {
const currentTime = process.uptime();

if (avatar == undefined) {
return;
}

if (currentTime - avatar.last_message_time > client_timeout_seconds) {
if (client && client.readyState === ws.OPEN) {
client.close(4001, 'Connection timed out');
}
}
}, 1000);
client.on("close", function (code, reason) {
console.log(shared.avatars);
console.log(`Client with UUID ${uuid} disconnected.)`);
clearInterval(interval);
if (uuid) {
const index = shared.avatars.findIndex((av) => av.uuid === uuid);
if (index > -1) {
Expand All @@ -182,9 +194,7 @@ wss.on("connection", function (client, request) {
function updateAllClients() {
// remove stale avatars:
let t = process.uptime();
// shared.avatars = shared.avatars.filter(
// (a) => t - a.last_message_time < client_timeout_seconds,
// );


// send all avatar data:
{
Expand Down Expand Up @@ -233,6 +243,7 @@ function handleLogin(msg, client, callback) {
}),
);
callback(user.uuid, avatar); // Update uuid and avatar in the closure
shared.avatars.push(avatar);
} else {
const newUser = createUser(username);
const newAvatar = createAvatar(newUser.uuid);
Expand Down