Awesome
Patchwire for iOS
iOS Library for the Patchwire multiplayer server framework created by Michael Barrett.
Requirements
- iOS 8.0+
- Xcode 7.3+
Installation
Carthage
Carthage is an easy to use dependency manager. Once you have installed Carthage, create a Cartfile with the following:
github "VictorBX/patchwire-ios"
CocoaPods
Coming soon
Manually
You can simply drag and drop the files within Source
into your project.
Usage
Once you have installed Patchwire-iOS into your project, we can start setting it up. Fist import patchwire.
import PatchwireiOS
Connecting to the server
Patchwire.sharedInstance.verboseLogging = false
Patchwire.sharedInstance.configure(serverIP: "localhost", serverPort: 3001)
Patchwire.sharedInstance.connect()
Sending a command
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)
Receiving a command
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.
Receiving IO Stream Events
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)
Disconnect/Reconnect
// To disconnect from the server
Patchwire.sharedInstance.disconnect()
// To reconnect to the server
Patchwire.sharedInstance.reconnect(connectAfterSeconds: 5)
Example Chat Project
Server Side
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 patchwire@0.2.0
if you haven't done so already - Run the server using
node example.js
Your server should now be running locally on localhost:3001
.
Client Side
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:
License
patchwire-ios is released under the MIT license. See LICENSE for details.