Member-only story
Naked Networking, SwiftUI and the game plan
Ok, so you just read the previous two articles on Naked Networking, if you missed them, here are some links.
Naked Networking with SwiftUI
More Naked Networking, more SwiftUI
The plan for this article then. In the first I outlined how to setup a listener for UDP packets, in the second we setup a transmitter to send UDP packets, both interfaced using the new SwiftUI framework. This time we’re going to be modifying our program to build a game. The game I have in mind ping pong, using two iOS devices.
Network
We’re going to start with the network framework changes first, of which there are almost none. We need to add just one more method to our class.
func stopListening() {
self.listening?.cancel()
}
This a simple routine to shutdown the listening interface on demand. In our game; it will, by default listen to port 1984; giving the option to close down that daemon, and bring up one listening to 4819. The idea is that idevice A will listen on 1984 and idevice B on 4819. They will send packets to their peer ports. So idevice 1984 will send a ping to 4819. And idevice 4819 will send a pong to 1984.
SwiftUI
Most of the changes are in the ContentView.swift file. Firstly we need to define a new class that we will associate with a toggle we will add to choose which side your on either 1984 or 4819. We’re also going to give our players…