From ca30d2e87dbcf0b453f309e0c80c19181e231453 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 6 Dec 2021 16:27:16 -0600 Subject: [PATCH] WIP peer list syncing --- lobby.gd | 12 +++++++----- server.ts | 23 +++++++++-------------- signaller_client.gd | 4 +++- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lobby.gd b/lobby.gd index 1670d47..b924133 100644 --- a/lobby.gd +++ b/lobby.gd @@ -41,11 +41,13 @@ func _peer_joined(joined_peers): peers.add_item("%s (%s)" % [name, peerId]) peers.set_item_metadata(peers.get_item_count() - 1, { "id": id }) -func _peer_left(id): - for i in range(peers.get_item_count()): - if id == peers.get_item_metadata(i)["id"]: - peers.remove_item(i) - return +func _peer_left(ids): + for data in ids: + var id = data["id"] + for i in range(peers.get_item_count()): + if id == peers.get_item_metadata(i)["id"]: + peers.remove_item(i) + return func _on_Button_pressed(): rpc("shift_cursor") diff --git a/server.ts b/server.ts index 94cb4a7..a7d8fc1 100644 --- a/server.ts +++ b/server.ts @@ -77,12 +77,13 @@ class Client { } } - clientList() { + peerList() { if (!this.lobby) return; - const netClients: { id: ID; name: string; peerId: number | null }[] = []; + const peers: { id: ID; name: string; peerId: number | null }[] = []; this.lobby.clients.forEach(({ id, name, peerId }) => - netClients.push({ id, name, peerId }) + peers.push({ id, name, peerId }) ); + this.send(buildMessage("peer_joined", peers)); // TODO: chunk async? } @@ -179,11 +180,11 @@ class Lobby { } client.send(buildMessage("your_peer_id", client.peerId.toString())); this.broadcast( - buildMessage("peer_joined", { + buildMessage("peer_joined", [{ id: client.id, name: client.name, peerId: client.peerId, - }), + }]), ); console.log("Sending peer_joined..."); client.send( @@ -197,15 +198,9 @@ class Lobby { this.clients.set(client.id, client); } - clientList() { - return Array.from(this.clients.values()).map( - ({ id, name, peerId }) => ({ id, name, peerId }), - ); - } - removeClient({ id }: Client) { this.clients.delete(id); - this.broadcast(buildMessage("peer_left", { id })); + this.broadcast(buildMessage("peer_left", [{ id }])); if (id === this.hostClientId) { console.warn("Host left!"); this.remove(); @@ -231,7 +226,7 @@ function onMessage(client: Client, ev: MessageEvent) { if (client.lobby == null) { client.send(`[info] not in a lobby`); } else { - client.send(buildMessage("peer_joined", client.lobby.clientList())); + client.peerList(); } } if (msg.startsWith("lobby_join:")) { @@ -239,7 +234,7 @@ function onMessage(client: Client, ev: MessageEvent) { const lobby = allLobbies.get(id); if (lobby) { client.lobbyJoin(lobby); - client.clientList(); + // client.peerList(); } else client.send(`[info] could not find lobby ${id}`); } if (msg.startsWith("json:")) { diff --git a/signaller_client.gd b/signaller_client.gd index 3e3504a..a5c6d0d 100644 --- a/signaller_client.gd +++ b/signaller_client.gd @@ -120,7 +120,9 @@ func handle_message(data: Dictionary): emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}]) else: emit_signal("peer_joined", data["data"]) - "peer_left": emit_signal("peer_left", data["id"]) + "peer_left": + print("Peer Left: %s" % data) + emit_signal("peer_left", data["data"]) "candidate": print("Candidate received - Data: %s" % JSON.print(data["data"]))