Skip to content

Commit

Permalink
Fix conflicting user id in user networking server (#130)
Browse files Browse the repository at this point in the history
* Fix conflicting user id

* Fix tests
  • Loading branch information
MarcusLongmuir authored Apr 30, 2024
1 parent 1dac27a commit b569a23
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions packages/3d-web-user-networking/src/UserNetworkingServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type UserNetworkingServerOptions = {
};

export class UserNetworkingServer {
private allClients = new Set<Client>();
private allClientsById = new Map<number, Client>();
private authenticatedClientsById: Map<number, Client> = new Map();

constructor(private options: UserNetworkingServerOptions) {
Expand All @@ -52,7 +52,7 @@ export class UserNetworkingServer {

private heartBeat() {
const now = Date.now();
this.allClients.forEach((client) => {
this.allClientsById.forEach((client) => {
if (now - client.lastPong > heartBeatRate) {
client.socket.close();
this.handleDisconnectedClient(client);
Expand All @@ -70,7 +70,7 @@ export class UserNetworkingServer {

private getId(): number {
let id = 1;
while (this.authenticatedClientsById.has(id)) {
while (this.allClientsById.has(id)) {
id++;
}
return id;
Expand All @@ -93,7 +93,7 @@ export class UserNetworkingServer {
state: 0,
},
};
this.allClients.add(client);
this.allClientsById.set(id, client);

socket.on("message", (message: WebSocket.Data, _isBinary: boolean) => {
if (message instanceof Buffer) {
Expand Down Expand Up @@ -147,10 +147,10 @@ export class UserNetworkingServer {
}

private handleDisconnectedClient(client: Client) {
if (!this.allClients.has(client)) {
if (!this.allClientsById.has(client.id)) {
return;
}
this.allClients.delete(client);
this.allClientsById.delete(client.id);
if (client.authenticatedUser !== null) {
// Only report disconnections of clients that were authenticated
this.options.onClientDisconnect(client.id);
Expand Down
8 changes: 4 additions & 4 deletions packages/3d-web-user-networking/test/UserNetworking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("UserNetworking", () => {
expect(await user1IdentityPromise).toEqual(1);

await waitUntil(
() => (server as any).allClients.size === 1,
() => (server as any).allClientsById.size === 1,
"wait for server to see the presence of user 1",
);

Expand Down Expand Up @@ -138,7 +138,7 @@ describe("UserNetworking", () => {
expect(await user2IdentityPromise).toEqual(2);

await waitUntil(
() => (server as any).allClients.size === 2,
() => (server as any).allClientsById.size === 2,
"wait for server to see the presence of user 2",
);

Expand Down Expand Up @@ -209,7 +209,7 @@ describe("UserNetworking", () => {
user2.stop();

await waitUntil(
() => (server as any).allClients.size === 1,
() => (server as any).allClientsById.size === 1,
"wait for server to see the removal of user 2",
);

Expand All @@ -221,7 +221,7 @@ describe("UserNetworking", () => {
user1.stop();

await waitUntil(
() => (server as any).allClients.size === 0,
() => (server as any).allClientsById.size === 0,
"wait for server to see the removal of user 1",
);

Expand Down

0 comments on commit b569a23

Please sign in to comment.