WebRTCMultiplayerPeer
A simple interface to create a peer-to-peer mesh network composed of WebRTCPeerConnection
that is compatible with the MultiplayerAPI
.
class WebRTCMultiplayerPeer
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(_:peerId:unreliableLifetime:)
or remove them via removePeer(peerId:)
. Peers must be added in WebRTCPeerConnection/ConnectionState/stateNew
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(peerId:channelsConfig:)
or createServer(channelsConfig:)
the isServerRelaySupported
method will return true
enabling peer exchange and packet relaying when supported by the MultiplayerAPI
implementation.
Superclasses
class MultiplayerPeer
Abstract class for specialized
PacketPeer
s used by theMultiplayerAPI
.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
Instance members
func addPeer(WebRTCPeerConnection?, peerId: Int32, unreliableLifetime: Int32
) -> GodotError Add a new peer to the mesh with the given
peerId
. TheWebRTCPeerConnection
must be in stateWebRTCPeerConnection/ConnectionState/stateNew
.func createClient(peerId: Int32, channelsConfig: GArray
) -> GodotError Initialize the multiplayer peer as a client with the given
peerId
(must be between 2 and 2147483647). In this mode, you should only calladdPeer(_:peerId:unreliableLifetime:)
once and withpeerId
of1
. This mode enablesisServerRelaySupported
, allowing the upperMultiplayerAPI
layer to perform peer exchange and packet relaying.func createMesh(peerId: Int32, channelsConfig: GArray
) -> GodotError 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).func createServer(channelsConfig: GArray
) -> GodotError Initialize the multiplayer peer as a server (with unique ID of
1
). This mode enablesisServerRelaySupported
, allowing the upperMultiplayerAPI
layer to perform peer exchange and packet relaying.func getPeer(peerId: Int32
) -> GDictionary Returns a dictionary representation of the peer with given
peerId
with three keys."connection"
containing theWebRTCPeerConnection
to this peer,"channels"
an array of threeWebRTCDataChannel
, and"connected"
a boolean representing if the peer connection is currently connected (all three channels are open).func getPeers(
) -> GDictionary Returns a dictionary which keys are the peer ids and values the peer representation as in
getPeer(peerId:)
.func hasPeer(peerId: Int32
) -> Bool Returns
true
if the givenpeerId
is in the peers map (it might not be connected though).func removePeer(peerId: Int32
) Remove the peer with given
peerId
from the mesh. If the peer was connected, and [signal MultiplayerPeer.peer_connected] was emitted for it, then [signal MultiplayerPeer.peer_disconnected] will be emitted.