简体   繁体   English

使用GKSession通过不同的WiFi网络连接2个设备

[英]connecting 2 devices over different WiFi network using GKSession

I am trying to connect 2 devices using GKSession. 我正在尝试使用GKSession连接2个设备。 When the devices are on same WiFi network it is working but when i am trying to connect devices on different WiFi network it is not working. 当设备在同一个WiFi网络上时,它正在工作,但当我尝试连接不同WiFi网络上的设备时,它无法正常工作。 This is my code for GKSession 这是我的GKSession代码

chatSession = [[GKSession alloc] initWithSessionID:AppName displayName:name sessionMode:GKSessionModePeer];
[chatSession setDataReceiveHandler:self withContext:nil];
chatSession.delegate = self;
chatSession.available = YES;



-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
NSLog(@"state=%d", state);
if(state == GKPeerStateDisconnected)
{
    // A peer disconnected
    chatSession.available = YES;
    NSLog(@"Disconnected");

    [data removeAllObjects];
    chatInput.hidden = YES;


}
else if(state == GKPeerStateConnected)
{
    // You can now send messages to the connected peer(s)


    NSData *imgData = UIImageJPEGRepresentation(myImage, 0.5);
    if(imgData==nil)
        NSLog(@"myImage is nil");

    NSError *err;
    //NSLog(@"pid=%@", pID);
    NSLog(@"before sending image");
    [chatSession sendData:imgData toPeers:[NSArray arrayWithObject:pID] withDataMode:GKSendDataReliable error:&err];
    NSLog(@"after send data");
    if(err)
        NSLog(@"error:%@", err.description);
}
else if (state == GKPeerStateAvailable)
{

    pID = peerID;
    [session connectToPeer:peerID withTimeout:60*120];
}

} }

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
// We can now decide to deny or accept
bool shouldAccept = YES;
if(shouldAccept)
{
    pID = peerID;
    [session acceptConnectionFromPeer:peerID error:nil];
}
else
{
    [session denyConnectionFromPeer:peerID];
}}

I am not sure if it is possible using GKSession. 我不确定是否可以使用GKSession。

According to Apple 根据Apple的说法

A GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi. GKSession对象提供使用蓝牙或Wi-Fi发现和连接到附近iOS设备的功能。

Please help me ia m trying to create a chat app using GKSession. 请帮助我尝试使用GKSession创建聊天应用程序。

iOS' GameKit most probably uses Bonjour for this, which works by doing broadcasts on a local network. iOS'GameKit最有可能使用Bonjour ,这可以通过在本地网络上进行广播来实现。 It won't work between two separate Wi-Fi networks. 它不适用于两个独立的Wi-Fi网络。

This means you'll need a central server (at least during connection set-up). 这意味着您将需要一个中央服务器(至少在连接设置期间)。 Have a look at XMPP , if you haven't done so already. 如果您还没有这样做,请查看XMPP

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM