mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 06:09:46 +03:00
Merge pull request #670 from jonbonazza/update-websocket-minimal
feat: update websocket-minimal to gdscript2.0
This commit is contained in:
@@ -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" )
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user