WIP peer list syncing

This commit is contained in:
Daniel Flanagan 2021-12-06 16:27:16 -06:00
parent dbe320ab34
commit ca30d2e87d
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 19 additions and 20 deletions

View file

@ -41,7 +41,9 @@ func _peer_joined(joined_peers):
peers.add_item("%s (%s)" % [name, peerId]) peers.add_item("%s (%s)" % [name, peerId])
peers.set_item_metadata(peers.get_item_count() - 1, { "id": id }) peers.set_item_metadata(peers.get_item_count() - 1, { "id": id })
func _peer_left(id): func _peer_left(ids):
for data in ids:
var id = data["id"]
for i in range(peers.get_item_count()): for i in range(peers.get_item_count()):
if id == peers.get_item_metadata(i)["id"]: if id == peers.get_item_metadata(i)["id"]:
peers.remove_item(i) peers.remove_item(i)

View file

@ -77,12 +77,13 @@ class Client {
} }
} }
clientList() { peerList() {
if (!this.lobby) return; 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 }) => 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? // TODO: chunk async?
} }
@ -179,11 +180,11 @@ class Lobby {
} }
client.send(buildMessage("your_peer_id", client.peerId.toString())); client.send(buildMessage("your_peer_id", client.peerId.toString()));
this.broadcast( this.broadcast(
buildMessage("peer_joined", { buildMessage("peer_joined", [{
id: client.id, id: client.id,
name: client.name, name: client.name,
peerId: client.peerId, peerId: client.peerId,
}), }]),
); );
console.log("Sending peer_joined..."); console.log("Sending peer_joined...");
client.send( client.send(
@ -197,15 +198,9 @@ class Lobby {
this.clients.set(client.id, client); this.clients.set(client.id, client);
} }
clientList() {
return Array.from(this.clients.values()).map(
({ id, name, peerId }) => ({ id, name, peerId }),
);
}
removeClient({ id }: Client) { removeClient({ id }: Client) {
this.clients.delete(id); this.clients.delete(id);
this.broadcast(buildMessage("peer_left", { id })); this.broadcast(buildMessage("peer_left", [{ id }]));
if (id === this.hostClientId) { if (id === this.hostClientId) {
console.warn("Host left!"); console.warn("Host left!");
this.remove(); this.remove();
@ -231,7 +226,7 @@ function onMessage(client: Client, ev: MessageEvent) {
if (client.lobby == null) { if (client.lobby == null) {
client.send(`[info] not in a lobby`); client.send(`[info] not in a lobby`);
} else { } else {
client.send(buildMessage("peer_joined", client.lobby.clientList())); client.peerList();
} }
} }
if (msg.startsWith("lobby_join:")) { if (msg.startsWith("lobby_join:")) {
@ -239,7 +234,7 @@ function onMessage(client: Client, ev: MessageEvent) {
const lobby = allLobbies.get(id); const lobby = allLobbies.get(id);
if (lobby) { if (lobby) {
client.lobbyJoin(lobby); client.lobbyJoin(lobby);
client.clientList(); // client.peerList();
} else client.send(`[info] could not find lobby ${id}`); } else client.send(`[info] could not find lobby ${id}`);
} }
if (msg.startsWith("json:")) { if (msg.startsWith("json:")) {

View file

@ -120,7 +120,9 @@ func handle_message(data: Dictionary):
emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}]) emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}])
else: else:
emit_signal("peer_joined", data["data"]) 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": "candidate":
print("Candidate received - Data: %s" % JSON.print(data["data"])) print("Candidate received - Data: %s" % JSON.print(data["data"]))