From d047c2de5bd79f26a3eaeaf250990f94f5e68846 Mon Sep 17 00:00:00 2001 From: Jon Bonazza Date: Tue, 7 Dec 2021 10:28:40 -0800 Subject: [PATCH] feat: update websocket-minimal to gdscript2.0 Update the networking/websocket-minimal project to use gdscript 2.0. --- networking/websocket_minimal/Main.tscn | 10 +++++----- networking/websocket_minimal/client.gd | 13 +++++++------ networking/websocket_minimal/project.godot | 3 ++- networking/websocket_minimal/server.gd | 12 ++++++------ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/networking/websocket_minimal/Main.tscn b/networking/websocket_minimal/Main.tscn index ab7297aa..37761d1f 100644 --- a/networking/websocket_minimal/Main.tscn +++ b/networking/websocket_minimal/Main.tscn @@ -1,12 +1,12 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=3 format=3 uid="uid://cxaa046x45suv"] -[ext_resource path="res://server.gd" type="Script" id=1] -[ext_resource path="res://client.gd" type="Script" id=2] +[ext_resource type="Script" path="res://server.gd" id="1"] +[ext_resource type="Script" path="res://client.gd" id="2"] [node name="Main" type="Node"] [node name="Server" type="Node" parent="."] -script = ExtResource( 1 ) +script = ExtResource( "1" ) [node name="Client" type="Node" parent="."] -script = ExtResource( 2 ) +script = ExtResource( "2" ) diff --git a/networking/websocket_minimal/client.gd b/networking/websocket_minimal/client.gd index d2544b56..96362177 100644 --- a/networking/websocket_minimal/client.gd +++ b/networking/websocket_minimal/client.gd @@ -1,20 +1,21 @@ extends Node # The URL we will connect to. -export var websocket_url = "ws://localhost:9080" +@export +var websocket_url = "ws://localhost:9080" # Our WebSocketClient instance. var _client = WebSocketClient.new() func _ready(): # Connect base signals to get notified of connection open, close, and errors. - _client.connect("connection_closed", self, "_closed") - _client.connect("connection_error", self, "_closed") - _client.connect("connection_established", self, "_connected") + _client.connect("connection_closed", _closed) + _client.connect("connection_error", _closed) + _client.connect("connection_established", _connected) # This signal is emitted when not using the Multiplayer API every time # a full packet is received. # Alternatively, you could check get_peer(1).get_available_packets() in a loop. - _client.connect("data_received", self, "_on_data") + _client.connect("data_received", _on_data) # Initiate connection to the given URL. var err = _client.connect_to_url(websocket_url) @@ -36,7 +37,7 @@ func _connected(proto = ""): print("Connected with protocol: ", proto) # You MUST always use get_peer(1).put_packet to send data to server, # and not put_packet directly when not using the MultiplayerAPI. - _client.get_peer(1).put_packet("Test packet".to_utf8()) + _client.get_peer(1).put_packet("Test packet".to_utf8_buffer()) func _on_data(): diff --git a/networking/websocket_minimal/project.godot b/networking/websocket_minimal/project.godot index 12be0e26..636f2cd0 100644 --- a/networking/websocket_minimal/project.godot +++ b/networking/websocket_minimal/project.godot @@ -6,13 +6,14 @@ ; [section] ; section goes between [] ; param=value ; assign values to parameters -config_version=4 +config_version=5 [application] config/name="WebSocket Minimal Demo" config/description="This is a minimal sample of connecting two peers to each other using websockets." run/main_scene="res://Main.tscn" +config/features=PackedStringArray("4.0") [rendering] diff --git a/networking/websocket_minimal/server.gd b/networking/websocket_minimal/server.gd index 66bd508b..6301fce4 100644 --- a/networking/websocket_minimal/server.gd +++ b/networking/websocket_minimal/server.gd @@ -8,14 +8,14 @@ var _server = WebSocketServer.new() func _ready(): # Connect base signals to get notified of new client connections, # disconnections, and disconnect requests. - _server.connect("client_connected", self, "_connected") - _server.connect("client_disconnected", self, "_disconnected") - _server.connect("client_close_request", self, "_close_request") + _server.connect("client_connected", _connected) + _server.connect("client_disconnected", _disconnected) + _server.connect("client_close_request", _close_request) # This signal is emitted when not using the Multiplayer API every time a # full packet is received. # Alternatively, you could check get_peer(PEER_ID).get_available_packets() # in a loop for each connected peer. - _server.connect("data_received", self, "_on_data") + _server.connect("data_received", _on_data) # Start listening on the given port. var err = _server.listen(PORT) if err != OK: @@ -23,10 +23,10 @@ func _ready(): set_process(false) -func _connected(id, proto): +func _connected(id, proto, rname): # This is called when a new peer connects, "id" will be the assigned peer id, # "proto" will be the selected WebSocket sub-protocol (which is optional) - print("Client %d connected with protocol: %s" % [id, proto]) + print("Client %d connected with protocol %s and resource name %s" % [id, proto, rname]) func _close_request(id, code, reason):