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

Fix conflicting user id in user networking server #130

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
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
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
Loading