Index · Object → RefCounted → PacketPeer → MultiplayerPeer → WebRTCMultiplayerPeer

WebRTCMultiplayerPeer

A simple interface to create a peer-to-peer mesh network composed of WebRTCPeerConnection that is compatible with the MultiplayerAPI.

Class Description

This class constructs a full mesh of WebRTCPeerConnection (one connection for each peer) that can be used as a MultiplayerAPI.multiplayer_peer.

You can add each WebRTCPeerConnection via add_peer() or remove them via remove_peer(). Peers must be added in WebRTCPeerConnection.STATE_NEW state to allow it to create the appropriate channels. This class will not create offers nor set descriptions, it will only poll them, and notify connections and disconnections.

When creating the peer via create_client() or create_server() the MultiplayerPeer.is_server_relay_supported() method will return true enabling peer exchange and packet relaying when supported by the MultiplayerAPI implementation.

Note: When exporting to Android, make sure to enable the INTERNET permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.

Properties

Inherited from MultiplayerPeer (3)
Inherited from PacketPeer (1)

Methods

func add_peer(peer: WebRTCPeerConnection, peer_id: int, unreliable_lifetime: int = 1) -> Error

Add a new peer to the mesh with the given peer_id. The WebRTCPeerConnection must be in state WebRTCPeerConnection.STATE_NEW.

Three channels will be created for reliable, unreliable, and ordered transport. The value of unreliable_lifetime will be passed to the "maxPacketLifetime" option when creating unreliable and ordered channels (see WebRTCPeerConnection.create_data_channel()).

func create_client(peer_id: int, channels_config: Array = []) -> Error

Initialize the multiplayer peer as a client with the given peer_id (must be between 2 and 2147483647). In this mode, you should only call add_peer() once and with peer_id of 1. This mode enables MultiplayerPeer.is_server_relay_supported(), allowing the upper MultiplayerAPI layer to perform peer exchange and packet relaying.

You can optionally specify a channels_config array of MultiplayerPeer.TransferMode which will be used to create extra channels (WebRTC only supports one transfer mode per channel).

func create_mesh(peer_id: int, channels_config: Array = []) -> Error

Initialize the multiplayer peer as a mesh (i.e. all peers connect to each other) with the given peer_id (must be between 1 and 2147483647).

func create_server(channels_config: Array = []) -> Error

Initialize the multiplayer peer as a server (with unique ID of 1). This mode enables MultiplayerPeer.is_server_relay_supported(), allowing the upper MultiplayerAPI layer to perform peer exchange and packet relaying.

You can optionally specify a channels_config array of MultiplayerPeer.TransferMode which will be used to create extra channels (WebRTC only supports one transfer mode per channel).

func get_peer(peer_id: int) -> Dictionary

Returns a dictionary representation of the peer with given peer_id with three keys. "connection" containing the WebRTCPeerConnection to this peer, "channels" an array of three WebRTCDataChannel, and "connected" a boolean representing if the peer connection is currently connected (all three channels are open).

func get_peers() -> Dictionary

Returns a dictionary which keys are the peer ids and values the peer representation as in get_peer().

func has_peer(peer_id: int) -> bool

Returns true if the given peer_id is in the peers map (it might not be connected though).

func remove_peer(peer_id: int) -> void

Remove the peer with given peer_id from the mesh. If the peer was connected, and MultiplayerPeer.peer_connected was emitted for it, then MultiplayerPeer.peer_disconnected will be emitted.

Inherited from MultiplayerPeer (11)
func close() -> void
func disconnect_peer(peer: int, force: bool = false) -> void
func get_unique_id() -> int
func poll() -> void
func set_target_peer(id: int) -> void
Inherited from PacketPeer (6)
func get_packet_error() -> Error
func get_var(allow_objects: bool = false) -> Variant
func put_packet(buffer: PackedByteArray) -> Error
func put_var(var: Variant, full_objects: bool = false) -> Error
Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from MultiplayerPeer (2)
signal peer_connected(id: int)
Inherited from Object (2)