I am developing a 3D Wi-Fi multiplayer game for iPhone and iPad without using any game engine. I have taken reference of the “WiTap” sample for device connectivity (Link to the sample code: WiTap sample)
I found that the devices are not able to receive the data sent by other device. I debugged my code and I found that the “NSStreamEventHasBytesAvailable” event is not triggered even if the other device has successfully written data to the stream. Also I found that when I close the streams from one device, the other device is not receiving “NSStreamEventEndEncountered” event on NSStreamDelegate.
The above issue only happens when I set “includesPeerToPeer” property to YES. I am setting this property to YES to allow faster discovery of devices (to avoid bug described here: link). I am testing my game on iPhone 5 (running on iOS 8.4) and iPad 3rd generation (running on iOS 7.1).
I have three questions:
- Why the above issue only happens when I set “includesPeerToPeer” property to YES?
- I think the above issue can happen if two devices are referring to different streams. Is there any method to confirm my assumption?
- Are there any other coding errors which can cause this issue?
I’d appreciate any suggestions on this issue. Thank you in advance.
Code snippets:
1. To search for nsnetservice
-(BOOL)searchForServicesOfType:(NSString *)type inDomain:(NSString *)domain {
self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];
self.netServiceBrowser.includesPeerToPeer = YES;
[self.netServiceBrowser setDelegate:self];
[self.netServiceBrowser searchForServicesOfType:type inDomain:domain];
[self.tableView reloadData];
return YES;
}
2. To publish nsnetservice
self.netService = [[NSNetService alloc] initWithDomain:domain type:protocol name:name port:0];
if(self.netService == nil)
return NO;
self.netService.includesPeerToPeer=YES;
[self.netService setDelegate:self];
[self.netService publishWithOptions:NSNetServiceListenForConnections];
3. To receive request on our net service
- (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)istr1 outputStream:(NSOutputStream *)ostr1 {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.netService stop];
[self.delegate didAcceptConnectionForServer:self inputStream:istr1 outputStream:ostr1];
}];
}
4. To send request to specific net service
BOOL success;
NSInputStream * inStream;
NSOutputStream * outStream;
success = [netService getInputStream:&inStream outputStream:&outStream];
if ( ! success ) {
[self setup_wifi];
} else {
_inStream = inStream;
_outStream = outStream;
isHost = true;
[self openStreams];
}