本文介绍了在 iPhone 之间传输数据的最佳选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个客户端-服务器架构,用于在多部 iPhone 之间传输数据.例如,服务器"iPhone 拥有动物的主列表.任意数量的客户端 iPhone 都可以连接到服务器 iPhone,然后读取和编辑列表.我试过的一些方法:

I would like to setup a client-server architecture for streaming data between multiple iPhones. For instance, the 'server' iPhone hosts a master list of animals. An arbitrary number of client iPhones can connect to the server iPhone then read and edit the list. Some methods I have tried:

  • 多点连接 - 最多仅支持 8 个客户端.如果有办法解决这个问题,这正是我要寻找的
  • GameKit - 我读到蓝牙连接在处理多个客户端时可能会出现问题
  • BLE - 蓝牙的特征值限制为 512 个八位字节.我假设动物列表在存档时可能会增长到大于最大特征值.
  • 套接字 - 我宁愿不必依赖外部服务器

我愿意接受hacky"解决方案.我正在考虑将每只动物作为一个单独的特征进行广播,但这可能会减慢发现速度,我觉得这会引起其他一些头痛.任何帮助将不胜感激

I'm willing to entertain 'hacky' solutions. I was thinking of broadcasting each animal as a separate characteristic, but this may slow down discovery and I have a feeling it will cause a few other headaches. Any help would be greatly appreciated

推荐答案

Multipeer Connectivity 仅支持每个会话 8 个对等点,但它支持多个会话.在您的情况下,如果有一个具有许多客户端的服务器"设备,并且客户端不需要看到彼此,服务器"可以根据需要创建新会话.

Multipeer Connectivity does only support 8 peers per session, but it supports multiple sessions. In your case, where there is a single 'server' device with many clients, and the clients don't need to see each other, the 'server' can just create new sessions as needed.

因此,当服务器"对等点充当广告商并接受邀请时,有一种方法可以返回现有会话或创建新会话:

So with the 'server' peer acting as advertiser, and accepting invitations, have a method that returns an existing session or creates a new one:

- (MCSession *)availableSession {

   //Try and use an existing session (_sessions is a mutable array)
   for (MCSession *session in _sessions)
       if ([session.connectedPeers count]<kMCSessionMaximumNumberOfPeers)
           return session;

    //Or create a new session
    MCSession *newSession = [self newSession];
    [_sessions addObject:newSession];

    return newSession;
}

- (MCSession *)newSession {

    MCSession *session = [[MCSession alloc] initWithPeer:_myPeerID securityIdentity:nil encryptionPreference:MCEncryptionNone];
    session.delegate = self;

    return session;
}

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {

    MCSession *session = [self availableSession];
    invitationHandler(YES,session);
}

然后我有这种发送方法:

I then have this method for sending:

- (void)sendData:(NSData *)data toPeers:(NSArray *)peerIDs reliable:(BOOL)reliable error:(NSError *__autoreleasing *)error {

    if ([peerIDs count]==0)
        return;

    NSPredicate *peerNamePred = [NSPredicate predicateWithFormat:@"displayName in %@", [peerIDs valueForKey:@"displayName"]];

    MCSessionSendDataMode mode = (reliable) ? MCSessionSendDataReliable : MCSessionSendDataUnreliable;

    //Need to match up peers to their session
    for (MCSession *session in _sessions){

        NSError __autoreleasing *currentError = nil;

        NSArray *filteredPeerIDs = [session.connectedPeers filteredArrayUsingPredicate:peerNamePred];

        [session sendData:data toPeers:filteredPeerIDs withMode:mode error:&currentError];

        if (currentError && !error)
            *error = currentError;
    }
}

当然可以对这种方法进行性能优化,但是就我向对等方发送数据的频率而言,这已经足够了.

There are certainly performance optimizations that can be made to this approach, but for the frequency with which I am sending data to peers this has worked well enough.

这篇关于在 iPhone 之间传输数据的最佳选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:14
查看更多