在iOS

时间:2016-06-16 05:15:30

标签: ios objective-c swift webrtc

我正在使用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");
    }
}

1 个答案:

答案 0 :(得分:4)

我可以通过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;

一旦两个设备都连接起来,我就检查了委托功能中的状态。频道的状态是开放的。

- (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