RTCDataChannel not working iOS

2019-05-26 09:05发布

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)

2条回答
可以哭但决不认输i
2楼-- · 2019-05-26 09:17

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

查看更多
混吃等死
3楼-- · 2019-05-26 09:17

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;
查看更多
登录 后发表回答