简体   繁体   English

GameKit:在没有实际连接的情况下使用GKSession发送额外信息?

[英]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. 我正在开发一个iOS应用程序,允许用户在本地与其他设备共享PDF。 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. 我遇到的问题是GKSession只允许在建立连接之前传输一个字符串 - 对等显示名称。 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. 例如,可以使用7个文件。 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'. 这可能需要相当长的时间,并且用户可能已经决定他们希望下载“文件1”。 By tapping 'file 1' a connection should be made and the transfer should begin. 通过点击“文件1”,应该建立连接并开始传输。 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? ). 我还看到了在断开连接后不久再次连接到服务器的一些问题..( GKSession在每次尝试时都没有连接? )。

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? 所以我想这可以归结为简单的问题 - 有没有人有任何想法我怎么能让客户端除了peerID和peerName之外还接收有关服务器的其他信息?

How about setting the Display Name in csv format and when you receive the csv name, only display the device name. 如何以csv格式设置显示名称,当您收到csv名称时,只显示设备名称。

Or you could send a JSON String of a NSDictionary as the display name 或者您可以发送NSDictionary的JSON字符串作为显示名称

Example Json to turn into NSString: 示例Json转为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. 然后,当您收到DisplayName时,仅显示DeviceName并解析某处的文件详细信息以便在以后发送对服务器的请求。

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 请记住,我不知道GKSession是否对显示名称长度有限制,因此此解决方案可能无法与大量文件一起使用

What I did was limit the display name to 16 characters and used the remaining 24 characters for my use. 我所做的是将显示名称限制为16个字符,并使用剩余的24个字符供我使用。

I created offsets from which the different information the app needs is read. 我创建了偏移量,从中读取了应用程序所需的不同信息。 For example 0 to 15 will represent the device name; 例如,0到15将代表设备名称; 16-24 will represent the file extension. 16-24将代表文件扩展名。 24 to 39 will represent the file name. 24到39将代表文件名。 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. 我编写了从40字符显示名称中获取这些字符串的方法。 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 干杯

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

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