Client / Server GKSessions

I have an application that is configured so that if the user selects the device as a server, he creates a GKSession in server mode, and if he selects cient, he creates a GKSession in client mode.

At the moment, it happens that the server sends the data in order, this is just a string containing the server time.

However, the problem occurs when I have one client already connected to the server, and then connect another client. The problem is this: the second connected client for some reason also connects to the first client.

I want to be able to not connect to other clients and just connect to the server. Any help would be great

+4
source share
1 answer

You hit a nail on the head:

Oddly enough, GK allows any "any" model.

This is truly an impressive achievement, technologically, Apple

BUT !!! ........ it is very important to realize that this is a very unusual option. On the net — for games — you almost always want a “normal” client-server model.

Over the course of about 30 years, a huge amount of science has become known regarding the creation of networks using the usual “talking” client-server model. Indeed, for game programming in 99% of cases, you definitely need a standard client-server "talking" model.

This is repeated:

(1) Apple created a very unusual peer-to-peer mode when they made GK.

(2) This is technologically astounding , but

(3) this new P2P technology would not actually be used in a very typical network programming for games.

(4) Very vaguely: all documentation talks about P2P mode. This is confusing because: if you are new to network programming, it will make you think that this is “normal”. In fact, it is very likely that you will never use P2P mode at all when you make real conventional games.

Repeat!

If you are new to the network , when you see all the mentions of P2P mode in Apple doco, it is important to understand that the (amazing) P2P function is more likely a novelty. In fact, for almost the entire gaming network, you definitely need the usual “everyday” match-style client-server network.

Just to crystal clear .. many, many, many! it’s easier to make a real network using a regular client-server “talking” model.

The P2P mode is really smart, and you can definitely spend some time experimenting with it. For example, it is normal to "send a chat" to each connected device.

But then again (if you are a new network programmer), you should understand that this is more likely a novelty. You will have to program using a regular, everyday, client-server model.

So...

given that you are going to program in a normal everyday client-server model.

Your first assignment becomes, oddly enough, the “disconnect” of the P2P aspect !!!!!

This is easy enough if you know how to do it. Here's exactly how to do it ...

sessionMode:GKSessionModeServer .. on the server. sessionMode:GKSessionModeClient .. on the client. 

So, for normal client-server programming, do not use GKSessionModePeer anywhere.

Secondly, do not forget about your clients, just do not implement didReceiveConnectionRequestFromPeer:

Finally, if all this does not work! In your clients, in peer: didChangeState :, you need to do this ...

 // so this is in the CLIENT... -(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { switch (state) { case GKPeerStateAvailable: // you have found the server!! // [session displayNameForPeer:peerID] self.theServerWeAreConnectedToPeerID = peerID; // you will definitely need to save that [session connectToPeer:theServerWeAreConnectedToPeerID withTimeout:0]; break; case GKPeerStateConnected: // MAKE SURE it our server // not just some bizarre peers, etc etc. if ( [theServerWeAreConnectedToPeerID isEqualToString:peerID] ) { // you are connected - launch your tick or whatever } else { // completely ignore this! } break; case GKPeerStateDisconnected: if ( [theServerWeAreConnectedToPeerID isEqualToString:peerID] ) { // you have been disconnected from the server!! } else { // weirdly this is telling you that one of your sister // clients, has been disconnected from the server. IGNORE } break; // GKPeerStateUnavailable .. likely not relevant case GKPeerStateUnavailable: // do nothing, ignore break; // from the modern doco.. // "The delegate should ignore GKPeerStateConnecting changes // and implement the session:didReceiveConnectionRequestFromPeer: // method instead." .. blah blah .. for us that comment only // applies TO OUR SERVER, NOT here in the client. case GKPeerStateConnecting: // do nothing, ignore break; } } 

Just repeat it. Most likely, you will want to NOT use the P2P model: so, all of the above explains "how to disable P2P material."

Repeat again for absolute clarity:

GK (unexpectedly) contains P2P mode. This is technologically amazing. Of course, you can use P2P mode for simple network problems (especially for chats). In practice, all the typical network needs of any real game (whether it be FPS, a sports game or something else) should be performed in the usual client-server paradigm . (It would be incredibly difficult to use P2P mode.)

Thus, it is necessary as a “first step” to know how to “not use” a P2P system in GK - this is explained above in length.

(If you want to use the P2P system - you are all set, go for it!)

GK is fantastic if you can figure out all the subtleties. With GK and ASIHttpRequest, you can basically do something between computers, at least in a basic way.

This question hit a nail on the head ........ GK has a (very unusual) P2P mode, and the documentation focuses on P2P mode , which can be confusing for beginners. Again, P2P is incredibly smart, but usually you don't need P2P mode. You need a normal client model with a talking server, which seems to be easier to program. Hope this helps!

+8
source

All Articles