From 949a9428eb9902c4b4b7ae3fc67b5a04328bae24 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sun, 28 Jul 2019 14:13:19 +0200 Subject: [PATCH] Add websocket minimal demo --- networking/websocket_minimal/Main.tscn | 21 +++++++++ networking/websocket_minimal/client.gd | 51 ++++++++++++++++++++ networking/websocket_minimal/project.godot | 19 ++++++++ networking/websocket_minimal/server.gd | 54 ++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 networking/websocket_minimal/Main.tscn create mode 100644 networking/websocket_minimal/client.gd create mode 100644 networking/websocket_minimal/project.godot create mode 100644 networking/websocket_minimal/server.gd diff --git a/networking/websocket_minimal/Main.tscn b/networking/websocket_minimal/Main.tscn new file mode 100644 index 00000000..02c63789 --- /dev/null +++ b/networking/websocket_minimal/Main.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://server.gd" type="Script" id=1] +[ext_resource path="res://client.gd" type="Script" id=2] + +[node name="Main" type="Node"] +__meta__ = { + +} + +[node name="Server" type="Node" parent="."] +script = ExtResource( 1 ) +__meta__ = { + +} + +[node name="Client" type="Node" parent="."] +script = ExtResource( 2 ) +__meta__ = { + +} diff --git a/networking/websocket_minimal/client.gd b/networking/websocket_minimal/client.gd new file mode 100644 index 00000000..6f52679f --- /dev/null +++ b/networking/websocket_minimal/client.gd @@ -0,0 +1,51 @@ +extends Node + +# The URL we will connect to +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") + # 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") + + # Initiate connection to the given URL. + var err = _client.connect_to_url(websocket_url) + if err != OK: + print("Unable to connect") + set_process(false) + +func _closed(was_clean = false): + # was_clean will tell you if the disconnection was correctly notified + # by the remote peer before closing the socket. + print("Closed, clean: ", was_clean) + set_process(false) + +func _connected(proto = ""): + # This is called on connection, "proto" will be the selected WebSocket + # sub-protocol (which is optional) + 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()) + +func _on_data(): + # Print the received packet, you MUST always use get_peer(1).get_packet + # to receive data from server, and not get_packet directly when not + # using the MultiplayerAPI. + print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8()) + +func _process(delta): + # Call this in _process or _physics_process. Data transfer, and signals + # emission will only happen when calling this function. + _client.poll() + +func _exit_tree(): + _client.disconnect_from_host() diff --git a/networking/websocket_minimal/project.godot b/networking/websocket_minimal/project.godot new file mode 100644 index 00000000..8afcdf7f --- /dev/null +++ b/networking/websocket_minimal/project.godot @@ -0,0 +1,19 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +_global_script_classes=[ ] +_global_script_class_icons={ + +} + +[application] + +config/name="WebSocket Minimal Demo" +run/main_scene="res://Main.tscn" diff --git a/networking/websocket_minimal/server.gd b/networking/websocket_minimal/server.gd new file mode 100644 index 00000000..07b7449d --- /dev/null +++ b/networking/websocket_minimal/server.gd @@ -0,0 +1,54 @@ +extends Node + +# The port we will listen to +const PORT = 9080 +# Our WebSocketServer instance +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") + # 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") + # Start listening on the given port. + var err = _server.listen(PORT) + if err != OK: + print("Unable to start server") + set_process(false) + +func _connected(id, proto): + # 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]) + +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]) + +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)]) + +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()]) + _server.get_peer(id).put_packet(pkt) + +func _process(delta): + # Call this in _process or _physics_process. + # Data transfer, and signals emission will only happen when calling this function. + _server.poll() + +func _exit_tree(): + _server.stop()