我正在使用ISBX/apprtc-ios代码进行视频聊天。这在iPhone和模拟器中完美工作。我想在两个同伴之间发送文本/字符串数据,并且正在使用RTCDataChannel类。

以下是我的实现,因此无法建立连接。它始终显示kRTCDataChannelStateConnecting状态。如何连接RTCDataChannel? iOS的WebRTC RTCDataChannel是否有可用的可行实现?

- (void)createNewDataChannel {
    if (self.clientDataChannel) {
        switch(self.clientDataChannel.state) {
            case kRTCDataChannelStateConnecting:
                NSLog(@"kRTCDataChannelStateConnecting");
                break;
            case kRTCDataChannelStateOpen:
                NSLog(@"kRTCDataChannelStateOpen");
                break;
            case kRTCDataChannelStateClosing:
                NSLog(@"kRTCDataChannelStateClosing");
                break;
            case kRTCDataChannelStateClosed:
                NSLog(@"kRTCDataChannelStateClosed");
                break;
            default:
                NSLog(@"Unknown");
        }
        return;
    }
    if (self.peerConnection == nil) {
        NSLog(@"Peerconnection is nil");
    }

    RTCDataChannelInit *DataChannelInit = [[RTCDataChannelInit alloc] init];
    DataChannelInit.maxRetransmits = 0;
    DataChannelInit.isOrdered=false;
    DataChannelInit.maxRetransmitTimeMs = -1;
    DataChannelInit.isNegotiated = false;
    DataChannelInit.streamId = 25;
    RTCDataChannel *dataChannel =[_peerConnection createDataChannelWithLabel:@"commands" config:DataChannelInit];
    dataChannel.delegate=self;
    self.clientDataChannel = dataChannel;

    if (self.clientDataChannel == nil) {
        NSLog(@"Datachannel is nil");
    }
    else {
        NSLog(@"Datachannel is working");
    }
}

最佳答案

我能够通过RTCDataChannel发送数据。我所做的是在发送要约之前。我使用以下配置创建了RTCDataChannelInit。

RTCDataChannelInit *datainit = [[RTCDataChannelInit alloc] init];

datainit.isNegotiated = YES;

datainit.isOrdered = YES;

datainit.maxRetransmits = 30;

datainit.maxRetransmitTimeMs = 30000;

datainit.streamId = 1;
self.dataChannel = [_peerConnection createDataChannelWithLabel:@"commands" config:datainit];
self.dataChannel.delegate=self;

一旦两个设备都连接好,我就检查了委托(delegate)函数中的状态。 channel 的状态为打开。
- (void)channelDidChangeState:(RTCDataChannel*)channel
{
    NSLog(@"channel.state %u",channel.state);
}

然后我按照下面的代码发送数据:
RTCDataBuffer *buffer = [[RTCDataBuffer alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] isBinary:NO];
BOOL x = [self.dataChannel sendData:buffer];

我使用的配置在此处给出:
https://groups.google.com/forum/#!searchin/discuss-webrtc/RTCDataChannel/discuss-webrtc/9NObqxnItCg/mRvXBIwkA7wJ

10-08 11:51