mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-04 14:11:02 +03:00
Merge branch 'master' into 3.2
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
.. _doc_high_level_multiplayer:
|
||||
|
||||
High level multiplayer
|
||||
High-level multiplayer
|
||||
======================
|
||||
|
||||
High level vs low level API
|
||||
High-level vs low-level API
|
||||
---------------------------
|
||||
|
||||
The following explains the differences of high- and low-level networking in Godot as well as some fundamentals. If you want to jump in head-first and add networking to your first nodes, skip to `Initializing the network`_ below. But make sure to read the rest later on!
|
||||
@@ -75,7 +75,7 @@ Initializing the network
|
||||
|
||||
The object that controls networking in Godot is the same one that controls everything tree-related: :ref:`SceneTree <class_SceneTree>`.
|
||||
|
||||
To initialize high level networking, the SceneTree must be provided a NetworkedMultiplayerPeer object.
|
||||
To initialize high-level networking, the SceneTree must be provided a NetworkedMultiplayerPeer object.
|
||||
|
||||
To create that object, it first has to be initialized as a server or client.
|
||||
|
||||
@@ -336,7 +336,7 @@ In most games, the goal of multiplayer networking is that the game runs synchron
|
||||
Besides supplying an RPC and remote member variable set implementation, Godot adds the concept of network masters.
|
||||
|
||||
Network master
|
||||
^^^^^^^^^^^^^^^^^^^^^^
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
The network master of a node is the peer that has the ultimate authority over it.
|
||||
|
||||
@@ -420,3 +420,17 @@ This may not make much sense for an area-of-effect case like the bomb, but in ot
|
||||
::
|
||||
|
||||
rpc_id(TARGET_PEER_ID, "stun") # Only stun the target peer
|
||||
|
||||
Exporting for dedicated servers
|
||||
-------------------------------
|
||||
|
||||
Once you've made a multiplayer game, you may want to export it to run it on
|
||||
a dedicated server with no GPU available. See
|
||||
:ref:`doc_exporting_for_dedicated_servers` for more information.
|
||||
|
||||
.. note::
|
||||
|
||||
The code samples on this page aren't designed to run on a dedicated
|
||||
server. You'll have to modify them so the server isn't considered to be a
|
||||
player. You'll also have to modify the game starting mechanism so that the
|
||||
first player who joins can start the game.
|
||||
|
||||
@@ -77,7 +77,7 @@ It will connect and fetch a website.
|
||||
else:
|
||||
# Or just plain Content-Length
|
||||
var bl = http.get_response_body_length()
|
||||
print("Response Length: ",bl)
|
||||
print("Response Length: ", bl)
|
||||
|
||||
# This method works for both anyway
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ which can take a regular connection and add security around it. The
|
||||
:ref:`HTTPClient <class_HTTPClient>`
|
||||
class also supports HTTPS by using this same wrapper.
|
||||
|
||||
For SSL to work, certificates need to be provided. A .crt file must be
|
||||
specified in the project settings:
|
||||
Godot includes SSL certificates from Mozilla, but you can provide your own
|
||||
with a .crt file in the project settings:
|
||||
|
||||
.. image:: img/ssl_certs.png
|
||||
|
||||
|
||||
@@ -28,10 +28,10 @@ This example will show you how to create a WebSocket connection to a remote serv
|
||||
::
|
||||
|
||||
extends Node
|
||||
|
||||
|
||||
# The URL we will connect to
|
||||
export var websocket_url = "ws://echo.websocket.org"
|
||||
|
||||
|
||||
# Our WebSocketClient instance
|
||||
var _client = WebSocketClient.new()
|
||||
|
||||
@@ -44,19 +44,19 @@ This example will show you how to create a WebSocket connection to a remote serv
|
||||
# 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)
|
||||
@@ -64,13 +64,13 @@ This example will show you how to create a WebSocket connection to a remote serv
|
||||
# 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.
|
||||
@@ -80,7 +80,7 @@ This will print:
|
||||
|
||||
::
|
||||
|
||||
Connected with protocol:
|
||||
Connected with protocol:
|
||||
Got data from server: Test packet
|
||||
|
||||
Minimal server example
|
||||
@@ -91,12 +91,12 @@ This example will show you how to create a WebSocket server that listen for remo
|
||||
::
|
||||
|
||||
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.
|
||||
@@ -113,30 +113,30 @@ This example will show you how to create a WebSocket server that listen for remo
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user