Heartbeat to prevent timeouts?

This commit is contained in:
Daniel Flanagan 2021-12-07 12:33:09 -06:00
parent dd94b53dbc
commit 8a5a05524e
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
4 changed files with 22 additions and 9 deletions

View file

@ -10,7 +10,7 @@ onready var chat_edit = $v/body/v/h/chat
func _ready(): func _ready():
Global.client.signaller.connect("peer_joined", self, "_peer_joined") Global.client.signaller.connect("peer_joined", self, "_peer_joined")
Global.client.signaller.connect("peer_left", self, "_peer_left") 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") 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())] 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") Global.client.signaller.connect("websocket_disconnected", self, "_signaller_disconnected")

View file

@ -4,8 +4,8 @@ extends Node
This module sets up WebRTC peer connections. This module sets up WebRTC peer connections.
""" """
var multiplayer_url = "wss://webrtc-signaller.deno.dev:443" # var multiplayer_url = "wss://webrtc-signaller.deno.dev:443"
# var multiplayer_url = "ws://localhost:8888" var multiplayer_url = "ws://localhost:8888"
# var multiplayer_url = "ws://echo.websocket.org" # var multiplayer_url = "ws://echo.websocket.org"
var webrtc_ice_servers = [ var webrtc_ice_servers = [
{ "urls": ["stun:stun.l.google.com:19302"] }, { "urls": ["stun:stun.l.google.com:19302"] },

View file

@ -18,9 +18,6 @@ type ServerDataObject = Record<string, string | number | symbol | null>;
type ServerData = ServerDataObject[] | ServerDataObject | string; type ServerData = ServerDataObject[] | ServerDataObject | string;
type Message = string | DataMessage; type Message = string | DataMessage;
const broadcast = (message: Message) =>
allClients.forEach((client) => client.send(message));
const buildMessage = ( const buildMessage = (
type: string, type: string,
data: ServerData | ServerData[], data: ServerData | ServerData[],
@ -72,11 +69,18 @@ class Client {
} }
} catch (e) { } catch (e) {
console.error( 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() { peerList() {
if (!this.lobby) return; if (!this.lobby) return;
const peers: { id: ID; name: string; peerId: number | null }[] = []; const peers: { id: ID; name: string; peerId: number | null }[] = [];
@ -165,11 +169,11 @@ class Lobby {
} }
remove() { remove() {
allLobbies.delete(this.id);
allClients.forEach((client) => client.lobbyDelete(this)); allClients.forEach((client) => client.lobbyDelete(this));
this.clients.forEach((client) => { this.clients.forEach((client) => {
client.lobbyLeave(); client.lobbyLeave();
}); });
allLobbies.delete(this.id);
} }
broadcast(message: Message) { broadcast(message: Message) {
@ -302,6 +306,13 @@ function onSocketError(client: Client, _ev: Event) {
onClientLeave(client); 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"); const PORT = parseInt(Deno.env.get("PORT") || "80");
console.log("Listening on port", PORT); console.log("Listening on port", PORT);
const listener = Deno.listen({ port: PORT }); const listener = Deno.listen({ port: PORT });

View file

@ -156,7 +156,9 @@ func handle_message(data: Dictionary):
"answer": "answer":
print("Answer received - Data: %s" % JSON.print(data["data"])) print("Answer received - Data: %s" % JSON.print(data["data"]))
emit_signal("answer_received", data["data"]) emit_signal("answer_received", data["data"])
"ping":
print("Signaller Ping")
_send("pong")
_: print("Unhandled Message - Data: %s" % JSON.print(data)) _: print("Unhandled Message - Data: %s" % JSON.print(data))
func send_candidate(peerId, mid, index, sdp) -> int: func send_candidate(peerId, mid, index, sdp) -> int: