diff --git a/lobby.gd b/lobby.gd index 8049520..20e9954 100644 --- a/lobby.gd +++ b/lobby.gd @@ -10,7 +10,7 @@ onready var chat_edit = $v/body/v/h/chat func _ready(): Global.client.signaller.connect("peer_joined", self, "_peer_joined") Global.client.signaller.connect("peer_left", self, "_peer_left") - Global.client.connect("peer_disconnected", self, "_peer_left") + # Global.client.connect("peer_disconnected", self, "_peer_left") Global.client.signaller.connect("lobby_left", self, "_lobby_left") lobby_info_label.text = "%s%s" % [Global.client.signaller.lobby_name, _host_suffix(get_tree().get_network_unique_id())] Global.client.signaller.connect("websocket_disconnected", self, "_signaller_disconnected") diff --git a/multiplayer_client.gd b/multiplayer_client.gd index 05fcd22..4dac680 100644 --- a/multiplayer_client.gd +++ b/multiplayer_client.gd @@ -4,8 +4,8 @@ extends Node This module sets up WebRTC peer connections. """ -var multiplayer_url = "wss://webrtc-signaller.deno.dev:443" -# var multiplayer_url = "ws://localhost:8888" +# var multiplayer_url = "wss://webrtc-signaller.deno.dev:443" +var multiplayer_url = "ws://localhost:8888" # var multiplayer_url = "ws://echo.websocket.org" var webrtc_ice_servers = [ { "urls": ["stun:stun.l.google.com:19302"] }, diff --git a/server.ts b/server.ts index d55b019..eb9af2b 100644 --- a/server.ts +++ b/server.ts @@ -18,9 +18,6 @@ type ServerDataObject = Record; type ServerData = ServerDataObject[] | ServerDataObject | string; type Message = string | DataMessage; -const broadcast = (message: Message) => - allClients.forEach((client) => client.send(message)); - const buildMessage = ( type: string, data: ServerData | ServerData[], @@ -72,11 +69,18 @@ class Client { } } catch (e) { console.error( - `Failed to send on socket ${this.socket} to client ${this.id}. Disconnecting and removing...`, + `Failed to send on socket ${this.socket} to client ${this.id}: ${ + JSON.stringify(e) + }`, ); + this.remove(); } } + ping() { + this.send(buildMessage("ping", {})); + } + peerList() { if (!this.lobby) return; const peers: { id: ID; name: string; peerId: number | null }[] = []; @@ -165,11 +169,11 @@ class Lobby { } remove() { + allLobbies.delete(this.id); allClients.forEach((client) => client.lobbyDelete(this)); this.clients.forEach((client) => { client.lobbyLeave(); }); - allLobbies.delete(this.id); } broadcast(message: Message) { @@ -302,6 +306,13 @@ function onSocketError(client: Client, _ev: Event) { onClientLeave(client); } +setInterval(() => { + allLobbies.forEach((lobby) => { + if (lobby.clients.size <= 0) lobby.remove(); + }); + allClients.forEach((client) => client.ping()); +}, 5000); + const PORT = parseInt(Deno.env.get("PORT") || "80"); console.log("Listening on port", PORT); const listener = Deno.listen({ port: PORT }); diff --git a/signaller_client.gd b/signaller_client.gd index f24ff85..45e8f67 100644 --- a/signaller_client.gd +++ b/signaller_client.gd @@ -156,7 +156,9 @@ func handle_message(data: Dictionary): "answer": print("Answer received - Data: %s" % JSON.print(data["data"])) emit_signal("answer_received", data["data"]) - + "ping": + print("Signaller Ping") + _send("pong") _: print("Unhandled Message - Data: %s" % JSON.print(data)) func send_candidate(peerId, mid, index, sdp) -> int: