CocoaMultipeer icon indicating copy to clipboard operation
CocoaMultipeer copied to clipboard

Enhancement: Support raw NSData, not just JSON

Open pkclsoft opened this issue 9 years ago • 4 comments

Having integrated the common code into a project in which I need to transmit binary data via NSData objects, the data is all being thrown away as the framework is expecting JSON only data.

I think, to be a truly useful, flexible framework it needs to support more than just JSON as a message package.

pkclsoft avatar Sep 21 '15 03:09 pkclsoft

The framework only expects JSON data for authentication purposes. After that its left up to you to determine what kinds of data packets you want to send over. See the demo, we are sending plain text encoded with NSUTF8StringEncoding, there is no JSON. If you are reading no JSON read throwing away garbage data in the console before your code is actually calling sendData it means something is going wrong with the authentication flow.

manavgabhawala avatar Sep 21 '15 04:09 manavgabhawala

What I do is wait for the connection state to become MGSessionStateConnected and then I send an NSData back to the peer. The thing is that both ends will do the same thing when they connect so that they are basically telling the other peer about themselves.

When the receiver receives the NSData I get:

"No JSON found. Throwing away garbage data."

So yes I can see that it's using JSON for the initial connection and handshake, but I don't see it choosing to let the data through after that.

The Demo has some problems in that I couldn't get it to send text from one side to the other successfully. I could see them connect, but no messages were displayed by the receiver if I typed into the other end.

pkclsoft avatar Sep 21 '15 04:09 pkclsoft

What's probably happening is something like this:

Requestor ---------------- (requests connection) ----------------> Receiver ----> asks you for auth Requestor <------------- (receives acceptance as JSON and extra "garbage" data together) ----- Receiver

So basically, before the requestor can receive the JSON that this has been accepted your data instance is "tacked" on before sending it to the requesting peer so it cannot parse the JSON. The easiest work around is probably to wait for a bit and send new data only after a couple of seconds after the MGSessionStateConnected is received. It chooses to let non JSON data through in the MGSession class after it has 'finalized' the connection. Hopefully, I can work around this so that this bug doesn't appear in the first non beta release of the framework.

As for the demo, you need to hit the return key for data to be sent over. It looks for the newline character and then sends the data over. I probably need to work on the demos a bit more to make them nicer but I have tested it and they do work (do not use it between the simulator and the mac) cause that may cause some problems but on an actual device and mac does work.

manavgabhawala avatar Sep 21 '15 04:09 manavgabhawala

OK, that's a possibility. I'll check that out and report back. RE the demo, I'd tried that; I'll try again. I had basically given up on the demo because of the problems with running it on iOS8.

pkclsoft avatar Sep 21 '15 06:09 pkclsoft