本文介绍了iOS中WebRTC的RTCDataChannel的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用代码进行视频聊天实施。这项工作在iPhone和模拟器中完美无缺。我想在两个对等体之间发送文本/字符串数据,我使用 RTCDataChannel 类。

I am using ISBX/apprtc-ios code for video chat implementation. This work perfect in iPhone and simulator. I want to send text/string data between two peers and I am using RTCDataChannel class.

以下是我的实施,我无法建立连接。它始终给出状态 kRTCDataChannelStateConnecting 如何连接RTCDataChannel?是否有适用于iOS的WebRTC RTCDataChannel的工作实现?

Following is my implementation and I am not able to establish the connection. It always give the status kRTCDataChannelStateConnecting How can I get the RTCDataChannel connected? Is there any working implementation available for WebRTC RTCDataChannel for iOS?

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

I am able to send data through RTCDataChannel. What I did is before sending the offer. I created the RTCDataChannelInit with the below configuration.

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;

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

Once both the devices get connected, I checked the state in the delegate function. The state of the channel is open.

- (void)channelDidChangeState:(RTCDataChannel*)channel
{
    NSLog(@"channel.state %u",channel.state);
}

然后我按照以下代码发送数据:

Then I send the data as per the below code:

RTCDataBuffer *buffer = [[RTCDataBuffer alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] isBinary:NO];
BOOL x = [self.dataChannel sendData:buffer];

我使用的配置如下:

这篇关于iOS中WebRTC的RTCDataChannel的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 14:43