iOS Library for the Patchwire multiplayer server framework created by Michael Barrett.
- iOS 8.0+
- Xcode 7.3+
Carthage is an easy to use dependency manager. Once you have installed Carthage, create a Cartfile with the following:
github "VictorBX/patchwire-ios"
Coming soon
You can simply drag and drop the files within Source
into your project.
Once you have installed Patchwire-iOS into your project, we can start setting it up. Fist import patchwire.
import PatchwireiOS
Patchwire.sharedInstance.verboseLogging = false
Patchwire.sharedInstance.configure(serverIP: "localhost", serverPort: 3001)
Patchwire.sharedInstance.connect()
With every command, you can send a dictionary containing some information.
let chatCommand = Command(command: "chat", data: ["username": "victor", "message": "hello"])
Patchwire.sharedInstance.sendCommand(chatCommand)
To receive incoming commands from the server, use NSNotificationCenter
.
var chatCommandKey = Patchwire.sharedInstance.notificationKey(forCommand: "chat")
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChatTableViewController.didReceiveChatCommand(_:)), name: chatCommandKey, object: nil)
The NSNotification
's userInfo
dictionary will contain the JSON blob sent from the server.
As the client sends and receives events from the server, the input and output streams send out a message when certain events are triggered. To get those NSStreamEvent
s from NSStreamDelegate
's - stream:handleEvent:
, you will need to use NSNotificationCenter
. A notification will be sent out by the notification center with names from the PatchwireStreamEventKey
enum. The userInfo
dictionary will contain the stream event.
// Receiving input stream events
NSNotificationCenter.defaultCenter().addObserver(self, selector: ViewController.didReceiveInputStreamEvent(_:), name: PatchwireStreamEventKey.InputStreamEvent.rawValue, object: nil)
// Receiving output stream events
NSNotificationCenter.defaultCenter().addObserver(self, selector: ViewController.didReceiveOutputStreamEvent(_:), name: PatchwireStreamEventKey.OutputStreamEvent.rawValue, object: nil)
// To disconnect from the server
Patchwire.sharedInstance.disconnect()
// To reconnect to the server
Patchwire.sharedInstance.reconnect(connectAfterSeconds: 5)
To run the example chat project, first let's setup the Patchwire server.
- Open a terminal and go to the
Example/server
directory - Install Patchwire using
npm install [email protected]
if you haven't done so already - Run the server using
node example.js
Your server should now be running locally on localhost:3001
.
Next setup the iOS client. For this example, we're using two iOS simulators that will have a build of the chat app.
- Open
Patchwire-iOS.xcodeproj
using Xcode - Select an iOS simulator (ex. iPhone 6s)
- Hit run (⌘R)
- Stop the simulator (⌘.)
- Repeat step 2, 3, and 4 but select another simulator (ex. iPhone 6s Plus)
- Close all simulators
- Open a terminal and:
cd /Applications/Xcode.app/Contents/Developer/Applications
- Type
open -n Simulator.app
- Repeat step 8 (a warning will pop up, ignore it)
- On the second simulator, under
Hardware > Device
select a device with the chat app installed (ex. if the first simulator is an iPhone 6s Plus, select iPhone 6s) - Run the chat app on both devices.
Result:
patchwire-ios is released under the MIT license. See LICENSE for details.