I'm currently working on the networking side of my SwiftUI app, and my question is, how would I go about making both devices both NWBrowsers and NWListeners at the same time? The first device to find an NWListener (through Bluetooth proximity and NWBrowser) would tell the other to cancel their NWBrowser, effectively becoming the server while the first one stays the client. Then, when the data transfer from the server to the client is done, they would switch places, so that the first phone can transfer their data to the second.
I don't have much experience in Networking, and the lack of explanative documentation for the Network Framework means the learning curve is very steep. Do I even need to do the server-client switching, or will NWConnection take care of that? Maybe a custom framing protocol, like what Apple showed in their WWDC presentation? I would be grateful for a few pointers or what direction I should take.
P.S: I know MultipeerConnectivity fits my needs and abstracts away many of the technicalities in networking, but it's unsupported (compared to Network) and doesn't offer the level of customisation that I'll need in the future (especially for Bluetooth proximity, which is how, when an NWListener gets very close, NWBrowser knows it must connect to it).
As to how you achieve your goal, there’s two complexities here:
You’ll want to use TCP and TCP APIs generally work in terms of byte streams. You’ll need a mechanism to send messages over that TCP byte stream. My preferred option here is to use the WebSocket support we added to Network framework in iOS 13.
The server/client switching approach you’d like is simply not supported at the TCP level, so you’ll have to implement this yourself. There isn’t a standard way of doing this but it’s not too hard. A good strategy is something like this:
Have each peer decide on its own unique ID.
When a client connects to a server, the first message it sends across that connection contains its unique ID.
The server then responds with its own unique ID.
If either end discovers that a connection with that unique ID pair already exists, it drops one of the connections. As long as they both agree to drop the same connection, everything will be fine. A good strategy here is to drop the one with the lesser ID.
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"