objective c - iOS - Wireless Communication Lagging -
so i'm making game involves wireless communication between multiple iphones, 1 being host. attempting via multipeerconnectivity framework, , i've made mcmanager class (an instance of put appdelegate it's available throughout app) handle sending data 1 system another. how sending implemented in code:
- (void) sendstate: (nsstring*) str; //used host send commands other connected systems { if(appdelegate.mcmanager.connected && iamhost){ nsdata *datatosend = [str datausingencoding: nsutf8stringencoding]; nsarray *allpeers = appdelegate.mcmanager.session.connectedpeers; nserror *error; [appdelegate.mcmanager.session senddata:datatosend topeers:allpeers withmode:mcsessionsenddatareliable error:&error]; if (error) { nslog(@"%@", [error localizeddescription]); } } }
and when subordinate systems receive data, mcmanager sends notification center notification , class, looking particular notification, grabs , executes this:
-(void)didreceivedatawithnotification:(nsnotification *)notification{ if(!iamhost){ nsdata *receiveddata = [[notification userinfo] objectforkey:@"data"]; nsstring *action = [[nsstring alloc] initwithdata: receiveddata encoding:nsutf8stringencoding]; nslog(@"recieved:"); nslog(action); //for debugging purposes, , figuring out timing //decide how act depending on string given if([action containsstring:@"changemaxscore"]){ //the string formatted as, example, "changemaxscore105" nsstring* valuestr = [action substringfromindex:14]; maxscore = (int)[valuestr integervalue]; [self changemaxscore]; //this method changes label text shows user value of maxscore } else if([action containsstring:@"changeplayerno"]){ //strings formatted "changeplayerno2" second segment in segmented control segments "2", "3", "4" //so referring 4 players nsstring *valuestr = [action substringfromindex:14]; [playernumbersegmentedcontrol setselectedsegmentindex: [valuestr integervalue]]; playernumber = playernumbersegmentedcontrol.selectedsegmentindex + 2; [self changeplayernumber]; //players can either human or type of ai (ai-1,ai-2,etc.) //the segmented control choose invisible unless player number playing //so method sets segmented control visible , interactable (by adding view) //and removes segmented controls not in use view } else if([action containsstring:@"changep0state"]){ //changes value of first player's segmented control (human, ai-1, ai-2, etc. nsstring* valuestr = [action substringfromindex:13]; aicontrol0.selectedsegmentindex = (int)[valuestr integervalue]; } ... else if([action containsstring:@"startgame"]) [self newgame]; //this method starts game and, in process, pushes view controller } }
my issue these actions, on receiving end, laggy. changing number of players, instance, receiver nslogs "received: changeplayerno1", , segmented control on-screen changes selected segment second one, stuff that's supposed show @ point...doesn't. , when send "startgame" command, receiver nslogs has received it, have wait thirty seconds start game asked.
this delay makes hard test whether wireless methods working or not (it works on host's side, because host changing them manually, not responsively - also, of works on other side of program, game without wireless support, several players/ais on single screen) and, if not fixed, prevent app being used easily.
i'm curious causes , can fix it. thank you!
Comments
Post a Comment