Table of Contents

Class WebRtcMultiplayerPeer

Namespace
Godot
Assembly
GodotSharp.dll

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

You can add each WebRtcPeerConnection via AddPeer(WebRtcPeerConnection, int, int) or remove them via RemovePeer(int). Peers must be added in 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 CreateClient(int, Array) or CreateServer(Array) the IsServerRelaySupported() 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.

[GodotClassName("WebRTCMultiplayerPeer")]
public class WebRtcMultiplayerPeer : MultiplayerPeer, IDisposable
Inheritance
WebRtcMultiplayerPeer
Implements
Inherited Members

Constructors

WebRtcMultiplayerPeer()

public WebRtcMultiplayerPeer()

Methods

AddPeer(WebRtcPeerConnection, int, int)

Add a new peer to the mesh with the given peerId. The WebRtcPeerConnection must be in state New.

Three channels will be created for reliable, unreliable, and ordered transport. The value of unreliableLifetime will be passed to the "maxPacketLifetime" option when creating unreliable and ordered channels (see CreateDataChannel(string, Dictionary)).

public Error AddPeer(WebRtcPeerConnection peer, int peerId, int unreliableLifetime = 1)

Parameters

peer WebRtcPeerConnection
peerId int
unreliableLifetime int

Returns

Error

CreateClient(int, Array)

Initialize the multiplayer peer as a client with the given peerId (must be between 2 and 2147483647). In this mode, you should only call AddPeer(WebRtcPeerConnection, int, int) once and with peerId of 1. This mode enables IsServerRelaySupported(), allowing the upper MultiplayerApi layer to perform peer exchange and packet relaying.

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

public Error CreateClient(int peerId, Array channelsConfig = null)

Parameters

peerId int
channelsConfig Array

Returns

Error

CreateMesh(int, Array)

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

public Error CreateMesh(int peerId, Array channelsConfig = null)

Parameters

peerId int
channelsConfig Array

Returns

Error

CreateServer(Array)

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

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

public Error CreateServer(Array channelsConfig = null)

Parameters

channelsConfig Array

Returns

Error

GetPeer(int)

Returns a dictionary representation of the peer with given peerId 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).

public Dictionary GetPeer(int peerId)

Parameters

peerId int

Returns

Dictionary

GetPeers()

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

public Dictionary GetPeers()

Returns

Dictionary

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

HasPeer(int)

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

public bool HasPeer(int peerId)

Parameters

peerId int

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

RemovePeer(int)

Remove the peer with given peerId from the mesh. If the peer was connected, and PeerConnected was emitted for it, then PeerDisconnected will be emitted.

public void RemovePeer(int peerId)

Parameters

peerId int