It works over the internet across clients

This commit is contained in:
Daniel Flanagan 2021-12-06 15:55:16 -06:00
parent 1417401ccd
commit 7d76876f99
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
4 changed files with 22 additions and 15 deletions

View file

@ -4,7 +4,7 @@ extends Node
This module sets up WebRTC peer connections. 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://localhost:8888"
# var multiplayer_url = "ws://echo.websocket.org" # var multiplayer_url = "ws://echo.websocket.org"
var webrtc_ice_servers = [ var webrtc_ice_servers = [

View file

@ -20,7 +20,11 @@ Global="*res://global.gd"
[gdnative] [gdnative]
singletons=[ ] singletons=[ "res://webrtc/webrtc.tres" ]
[network]
ssl/certificates="res://ca-certificates.crt"
[physics] [physics]

View file

@ -15,7 +15,7 @@ interface DataMessage {
} }
type ServerDataObject = Record<string, string | number | symbol | null>; type ServerDataObject = Record<string, string | number | symbol | null>;
type ServerData = ServerDataObject | string; type ServerData = ServerDataObject[] | ServerDataObject | string;
type Message = string | DataMessage; type Message = string | DataMessage;
const broadcast = (message: Message) => const broadcast = (message: Message) =>
@ -185,12 +185,13 @@ class Lobby {
peerId: client.peerId, peerId: client.peerId,
}), }),
); );
this.clients.forEach((c) => client.send(
client.send(buildMessage("peer_joined", { buildMessage(
id: c.id, "peer_joined",
name: c.name, Array.from(this.clients.values()).map(
peerId: c.peerId, ({ id, name, peerId }) => ({ id, name, peerId }),
})) ),
),
); );
this.clients.set(client.id, client); this.clients.set(client.id, client);
} }

View file

@ -30,9 +30,14 @@ func _ready():
var _result = ws.connect("data_received", self, "_parse_msg") var _result = ws.connect("data_received", self, "_parse_msg")
_result = ws.connect("connection_established", self, "_connected") _result = ws.connect("connection_established", self, "_connected")
_result = ws.connect("connection_closed", self, "_closed") _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") _result = ws.connect("server_close_request", self, "_close_request")
func _succ():
print("WebSocket Connection Succeeded")
func close(): func close():
ws.disconnect_from_host() ws.disconnect_from_host()
@ -60,11 +65,8 @@ func _connected(protocol = ""):
func _process(_delta: float): func _process(_delta: float):
var status: int = ws.get_connection_status() var status: int = ws.get_connection_status()
if status in [WebSocketClient.CONNECTION_CONNECTED, WebSocketClient.CONNECTION_CONNECTING]:
ws.poll() ws.poll()
if status == WebSocketClient.CONNECTION_CONNECTED || status == WebSocketClient.CONNECTION_CONNECTING:
print("I'm polling! %s" % status)
else:
print("Not polling! %s" % status)
func join_lobby(id: String): func join_lobby(id: String):
return _send("lobby_join:%s" % id) return _send("lobby_join:%s" % id)