webrtc-android icon indicating copy to clipboard operation
webrtc-android copied to clipboard

DataChannel example

Open mdrlzy opened this issue 2 years ago • 2 comments
trafficstars

Looks like PeerConnection.createDataChannel does not trigger PeerConnection.Observer.onDataChannel Maybe I'm doing something wrong So could you provide small example?

mdrlzy avatar Aug 02 '23 15:08 mdrlzy

It will be triggered after the two peers establish a connection

ApplY3D avatar Nov 12 '23 13:11 ApplY3D

also you could set negotiated=true and id=0 to all your peers and you will not need onDataChannel anymore

val dataChannelConfig = DataChannel.Init()
dataChannelConfig.negotiated = true
dataChannelConfig.id = 0
dataChannel = localPeer?.createDataChannel("ch0", dataChannelConfig)
dataChannel?.registerObserver(object : DataChannel.Observer {
    override fun onBufferedAmountChange(p0: Long) {}
    override fun onMessage(buffer: DataChannel.Buffer?) {
        if (buffer == null) return
        val msg = StandardCharsets.UTF_8.decode(buffer.data)
    }

    override fun onStateChange() {
        if (dataChannel?.state() == DataChannel.State.OPEN) {
            // peers connected, channel open
        }
    }
})

ApplY3D avatar Nov 13 '23 12:11 ApplY3D