WebRTCPeerConnection

Inherits: Reference < Object

继承人: WebRTCPeerConnectionGDNative

类别: 核心

简要说明

与WebRTC对等连接的接口。

信号

  • data_channel_received ( Object channel )

当接收到新的带内信道时发出,即当使用 negotiated: false (默认)。

该对象将是 WebRTCDataChannel . 您必须保留对它的引用,否则它将自动关闭。见 create_data_channel

在创建新的冰候选时发出。这三个参数将通过信令服务器传递给远程对等机。

在成功调用后发出 create_offerset_remote_description (当它生成一个答案时)。参数将传递给 set_local_description 并通过信令服务器发送到远程对等机。

枚举

枚举 ConnectionState

  • STATE_NEW = 0 ---连接是新的,可以在此状态下创建数据通道和报价。

  • STATE_CONNECTING = 1 ---对等机正在连接,ICE正在进行,没有一个传输失败。

  • STATE_CONNECTED = 2 ---对等机已连接,所有冰运输均已连接。

  • STATE_DISCONNECTED = 3 ---至少有一个冰运输中断。

  • STATE_FAILED = 4 ---一个或多个冰运输失败。

  • STATE_CLOSED = 5 ---对等连接已关闭(调用后 close 例如)。

描述

本地计算机和远程对等机之间的WebRTC连接。提供连接、维护和监视连接的接口。

从现在起,在两个对等机之间建立WebRTC连接似乎不是一项简单的任务,但它可以分为三个主要步骤:

  • 要启动连接的对等机 (A 从现在起)创建一个报价并发送给其他同行 (B 从现在起)。

  • B 接收报价,生成并回答,然后发送给 A

  • AB 然后互相生成和交换候选冰。

完成这些步骤后,连接应该连接起来。继续阅读或查看教程了解更多信息。

方法说明

添加由远程对等机生成的ICE候选(并通过信令服务器接收)。见 ice_candidate_created .

  • void close ( )

关闭对等连接和与之关联的所有数据通道。注意,除非调用 initialize .

返回新的 WebRTCDataChannel (或) null 失败时)与给定 label 并可通过 options 字典。只有在连接处于状态时才能调用此方法 STATE_NEW .

创建工作数据通道有两种方法:调用 create_data_channel 只在其中一个同伴身上听 data_channel_received 在另一个,或者打电话 create_data_channel 在具有相同值的两个对等机上,以及 negotiated 选项设置为 true .

有效的 options 是:

{
    "negotiated": true, # When set to true (default off), means the channel is negotiated out of band. "id" must be set too. data_channel_received will not be called.
    "id": 1, # When "negotiated" is true this value must also be set to the same value on both peer.

    # Only one of maxRetransmits and maxPacketLifeTime can be specified, not both. They make the channel unreliable (but also better at real time).
    "maxRetransmits": 1, # Specify the maximum number of attempt the peer will make to retransmits packets if they are not acknowledged.
    "maxPacketLifeTime": 100, # Specify the maximum amount of time before giving up retransmitions of unacknowledged packets (in milliseconds).
    "ordered": true, # When in unreliable mode (i.e. either "maxRetransmits" or "maxPacketLifetime" is set), "ordered" (true by default) specify if packet ordering is to be enforced.

    "protocol": "my-custom-protocol", # A custom sub-protocol string for this channel.
}

注: 您必须保留对以这种方式创建的频道的引用,否则将关闭该频道。

创建新的SDP提供以启动与远程对等机的WebRTC连接。至少一个 WebRTCDataChannel 必须在调用此方法之前创建。

如果此函数返回 @GlobalScope.OKsession_description_created 将在会话准备发送时调用。

返回连接状态。见 ConnectionState .

重新初始化此对等连接,关闭任何以前活动的连接,然后返回状态 STATE_NEW . 字典 options 可以传递以配置对等连接。

有效的 options 是:

{
    "iceServers": [
        {
            "urls": [ "stun:stun.example.com:3478" ], # One or more STUN servers.
        },
        {
            "urls": [ "turn:turn.example.com:3478" ], # One or more TURN servers.
            "username": "a_username", # Optional username for the TURN server.
            "credentials": "a_password", # Optional password for the TURN server.
        }
    ]
}

经常调用此方法(例如 Node._processNode._physics_process )正确接收信号。

设置本地对等机的SDP描述。应调用此函数以响应 session_description_created .

如果 typeanswer 对等端将开始发出 ice_candidate_created .

设置远程对等机的SDP描述。应该使用远程对等机生成的值来调用它,并通过信令服务器接收这些值。

如果 typeoffer 对等端将发出 session_description_created 有适当的答案。

如果 typeanswer 对等端将开始发出 ice_candidate_created .