RTCDataChannel not working iOS

2019-05-26 09:24发布

问题:

I'm using RTCDataChannel. But messages which i'm sending through the channel are not receiving at the other peer.

Here is the code:

        let audioConstraint : RTCPair = RTCPair(key: "OfferToReceiveAudio", value: "true")
        let videoConstraint : RTCPair = RTCPair(key: "OfferToReceiveVideo", value: "true")
        let dtlsConstraint : RTCPair = RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")
        let mediaContraints : RTCMediaConstraints = RTCMediaConstraints(mandatoryConstraints: [audioConstraint, videoConstraint], optionalConstraints: [ dtlsConstraint])

        RTCPeerConnectionFactory.initializeSSL()
        peerConnection = peerConnectionFactory.peerConnectionWithICEServers(servers, constraints: mediaContraints, delegate: self)

        dataChannels = peerConnection?.createDataChannelWithLabel(channelName,config: nil)
        dataChannels?.delegate = self


        var message : NSData = NSData(base64EncodedString: "helloo")
        var buffer : RTCDataBuffer = RTCDataBuffer(data: message, isBinary: true)
        dataChannels?.sendData(buffer)

回答1:

Have you resolved it? One of the two peers should create data channel and other should attach the received data channel object to its data channel object. The initiator should create datachannel before sending offer. Hope this might be helpful



回答2:

I had the same problem until I set the option not to nil. If I skipped the steamId, it would not send. Even though the channel is open.

RTCDataChannelInit *dataInit = [[RTCDataChannelInit alloc] init];
dataInit.isNegotiated = YES;
dataInit.isOrdered = YES;
dataInit.maxRetransmits = 30;
dataInit.maxRetransmitTimeMs = 30000;
dataInit.streamId = 12;  //important setting
self.dataChannel = [_peerConnection createDataChannelWithLabel:kRTCDataChannelLabel config:dataInit];
self.dataChannel.delegate = self;