WebRTCMultiplayerPeer

Inherits: MultiplayerPeer < PacketPeer < RefCounted < Object

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

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.

MultiplayerPeer.connection_succeeded and MultiplayerPeer.server_disconnected will not be emitted unless server_compatibility is true in initialize. Beside that data transfer works like in a MultiplayerPeer.

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.

Methods

Error

add_peer ( WebRTCPeerConnection peer, int peer_id, int unreliable_lifetime=1 )

void

close ( )

Dictionary

get_peer ( int peer_id )

Dictionary

get_peers ( )

bool

has_peer ( int peer_id )

Error

initialize ( int peer_id, bool server_compatibility=false, Array channels_config=[] )

void

remove_peer ( int peer_id )

Method Descriptions

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).


  • void close ( )

Close all the add peer connections and channels, freeing all resources.


Return 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).


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


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


  • Error initialize ( int peer_id, bool server_compatibility=false, Array channels_config=[] )

Initialize the multiplayer peer with the given peer_id (must be between 1 and 2147483647).

If server_compatibilty is false (default), the multiplayer peer will be immediately in state MultiplayerPeer.CONNECTION_CONNECTED and MultiplayerPeer.connection_succeeded will not be emitted.

If server_compatibilty is true the peer will suppress all MultiplayerPeer.peer_connected signals until a peer with id MultiplayerPeer.TARGET_PEER_SERVER connects and then emit MultiplayerPeer.connection_succeeded. After that the signal MultiplayerPeer.peer_connected will be emitted for every already connected peer, and any new peer that might connect. If the server peer disconnects after that, signal MultiplayerPeer.server_disconnected will be emitted and state will become MultiplayerPeer.CONNECTION_CONNECTED.

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


  • void remove_peer ( int peer_id )

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.