From 7d76876f994cee6cafb0a7ae091e494ea12a32ca Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 6 Dec 2021 15:55:16 -0600 Subject: [PATCH] It works over the internet across clients --- multiplayer_client.gd | 2 +- project.godot | 6 +++++- server.ts | 15 ++++++++------- signaller_client.gd | 14 ++++++++------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/multiplayer_client.gd b/multiplayer_client.gd index 0146c8b..fbd163c 100644 --- a/multiplayer_client.gd +++ b/multiplayer_client.gd @@ -4,7 +4,7 @@ extends Node This module sets up WebRTC peer connections. """ -var multiplayer_url = "ws://webrtc-signaller.deno.dev" +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 = [ diff --git a/project.godot b/project.godot index f160e0a..6766717 100644 --- a/project.godot +++ b/project.godot @@ -20,7 +20,11 @@ Global="*res://global.gd" [gdnative] -singletons=[ ] +singletons=[ "res://webrtc/webrtc.tres" ] + +[network] + +ssl/certificates="res://ca-certificates.crt" [physics] diff --git a/server.ts b/server.ts index 6887c0e..7a69288 100644 --- a/server.ts +++ b/server.ts @@ -15,7 +15,7 @@ interface DataMessage { } type ServerDataObject = Record; -type ServerData = ServerDataObject | string; +type ServerData = ServerDataObject[] | ServerDataObject | string; type Message = string | DataMessage; const broadcast = (message: Message) => @@ -185,12 +185,13 @@ class Lobby { peerId: client.peerId, }), ); - this.clients.forEach((c) => - client.send(buildMessage("peer_joined", { - id: c.id, - name: c.name, - peerId: c.peerId, - })) + client.send( + buildMessage( + "peer_joined", + Array.from(this.clients.values()).map( + ({ id, name, peerId }) => ({ id, name, peerId }), + ), + ), ); this.clients.set(client.id, client); } diff --git a/signaller_client.gd b/signaller_client.gd index 8c53572..66f9cf7 100644 --- a/signaller_client.gd +++ b/signaller_client.gd @@ -30,9 +30,14 @@ func _ready(): var _result = ws.connect("data_received", self, "_parse_msg") _result = ws.connect("connection_established", self, "_connected") _result = ws.connect("connection_closed", self, "_closed") - _result = ws.connect("connection_error", self, "_closed") + _result = ws.connect("connection_error", self, "_closed", [false]) + _result = ws.connect("connection_failed", self, "_closed", [false]) + _result = ws.connect("connection_succeeded", self, "_succ") _result = ws.connect("server_close_request", self, "_close_request") +func _succ(): + print("WebSocket Connection Succeeded") + func close(): ws.disconnect_from_host() @@ -60,11 +65,8 @@ func _connected(protocol = ""): func _process(_delta: float): var status: int = ws.get_connection_status() - ws.poll() - if status == WebSocketClient.CONNECTION_CONNECTED || status == WebSocketClient.CONNECTION_CONNECTING: - print("I'm polling! %s" % status) - else: - print("Not polling! %s" % status) + if status in [WebSocketClient.CONNECTION_CONNECTED, WebSocketClient.CONNECTION_CONNECTING]: + ws.poll() func join_lobby(id: String): return _send("lobby_join:%s" % id)