bencallis
bencallis

Reputation: 3669

GameKit: Send extra information with GKSession without actual connection?

I am working on an iOS application which allows users to share PDFs with other devices locally. When the application launches the device actively becomes a client searching for servers who are broadcasting files. When the client sees a server it adds a badge to its tab and adds an entry to it's table.

The problem I am having is the GKSession only allows for one string to be transmitted before the connection is established -The peer Display Name. What I would like to transmit is the file name, number of pages and maybe a thumbnail.

The only way I can currently think of to achieve this behaviour is to automatically connect the client and servers, exchange the file information and then automatically disconnect them. This may work well if there is only one or two files currently broadcasting, there is likely to be threading/synchronisation issues when lots of different devices are broadcasting.

For example 7 files may be available. So the client would have to connect to them one by one in order to receive information for all the files. This is likely to take a fair amount of time and the user may have already decided that they wish to download 'file 1'. By tapping 'file 1' a connection should be made and the transfer should begin. This may not be able to happen instantly if the client is busy retrieving information about the other available files.

I have also read some issues with connecting to a server again shortly after disconnecting from it.. (what GKSession doesn't connect on every attempt?).

So I guess it boils down to the simple questions - Does anyone have any ideas how I could allow the client to receive additional information about the server other than just the peerID and peerName?

Upvotes: 3

Views: 419

Answers (2)

pnizzle
pnizzle

Reputation: 6431

What I did was limit the display name to 16 characters and used the remaining 24 characters for my use.

I created offsets from which the different information the app needs is read. For example 0 to 15 will represent the device name; 16-24 will represent the file extension. 24 to 39 will represent the file name. If a string is longer than its respective space then its cut off, or truncated depending on what you prefer. If a string is shorter than its respective space then it is padded with spaces. I have written methods to get these strings from the 40 character display name. If anyone has a better solution I would be happy to here them. Can not post code at the moment as I don't have access to it.

Cheers

Upvotes: 0

CStreel
CStreel

Reputation: 2642

How about setting the Display Name in csv format and when you receive the csv name, only display the device name.

Or you could send a JSON String of a NSDictionary as the display name

Example Json to turn into NSString:

{
     "DeviceName" : "Example",
     "File" : [
            {
                "FileName" : "PDF1"
                "TotalPages" : 100
            }
            ]
}

Stringified version:

{"DeviceName" : "Example","File" : [{"FileName" : "PDF1","TotalPages" : 100}]}

Then when you receive the DisplayName, only show the DeviceName and parse the File Details somewhere to send requests for the server at a later date.

Keep in mind I don't know if GKSession has a limit on the Display Name length so this solution may not work with a large amount of files

Upvotes: 2

Related Questions