mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-07 00:10:09 +01:00
Update most demos for Godot 4.0.beta10 (#782)
This commit is contained in:
@@ -8,18 +8,18 @@ var _client = WebSocketClient.new()
|
||||
|
||||
func _ready():
|
||||
# Connect base signals to get notified of connection open, close, and errors.
|
||||
_client.connect(&"connection_closed", _closed)
|
||||
_client.connect(&"connection_error", _closed)
|
||||
_client.connect(&"connection_established", _connected)
|
||||
_client.connection_closed.connect(self._closed)
|
||||
_client.connection_error.connect(self._closed)
|
||||
_client.connection_established.connect(self._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", _on_data)
|
||||
_client.data_received.connect(self._on_data)
|
||||
|
||||
# Initiate connection to the given URL.
|
||||
var err = _client.connect_to_url(websocket_url)
|
||||
if err != OK:
|
||||
print("Unable to connect")
|
||||
push_error("Unable to connect.")
|
||||
set_process(false)
|
||||
|
||||
|
||||
|
||||
@@ -8,45 +8,45 @@ var _server = WebSocketServer.new()
|
||||
func _ready():
|
||||
# Connect base signals to get notified of new client connections,
|
||||
# disconnections, and disconnect requests.
|
||||
_server.connect(&"client_connected", _connected)
|
||||
_server.connect(&"client_disconnected", _disconnected)
|
||||
_server.connect(&"client_close_request", _close_request)
|
||||
_server.client_connected.connect(self._connected)
|
||||
_server.client_disconnected.connect(self._disconnected)
|
||||
_server.client_close_request.connect(self._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", _on_data)
|
||||
_server.data_received.connect(self._on_data)
|
||||
# Start listening on the given port.
|
||||
var err = _server.listen(PORT)
|
||||
if err != OK:
|
||||
print("Unable to start server")
|
||||
push_error("Unable to start server.")
|
||||
set_process(false)
|
||||
|
||||
|
||||
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 and resource name %s" % [id, proto, rname])
|
||||
print_rich("Client [b]%d[/b] [color=green]connected[/color] with protocol [b]%s[/b] and resource name [b]%s[/b]" % [id, proto, rname])
|
||||
|
||||
|
||||
func _close_request(id, code, reason):
|
||||
# This is called when a client notifies that it wishes to close the connection,
|
||||
# providing a reason string and close code.
|
||||
print("Client %d disconnecting with code: %d, reason: %s" % [id, code, reason])
|
||||
print_rich("Client [b]%d[/b] [color=yellow]disconnecting[/color] with code: [b]%d[/b], reason: [b]%s[/b]" % [id, code, reason])
|
||||
|
||||
|
||||
func _disconnected(id, was_clean = false):
|
||||
# This is called when a client disconnects, "id" will be the one of the
|
||||
# disconnecting client, "was_clean" will tell you if the disconnection
|
||||
# was correctly notified by the remote peer before closing the socket.
|
||||
print("Client %d disconnected, clean: %s" % [id, str(was_clean)])
|
||||
print_rich("Client [b]%d[/b] [color=red]disconnected[/color], clean: [b]%s[/b]" % [id, str(was_clean)])
|
||||
|
||||
|
||||
func _on_data(id):
|
||||
# Print the received packet, you MUST always use get_peer(id).get_packet to receive data,
|
||||
# and not get_packet directly when not using the MultiplayerAPI.
|
||||
var pkt = _server.get_peer(id).get_packet()
|
||||
print("Got data from client %d: %s ... echoing" % [id, pkt.get_string_from_utf8()])
|
||||
print("Got data from client [b]%d[/b]: [b]%s[/b] ... echoing" % [id, pkt.get_string_from_utf8()])
|
||||
_server.get_peer(id).put_packet(pkt)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user