 SocketIO-Kit
SocketIO-Kit is a Socket.io iOS client with type safe, clean syntax and speed in mind. WebSocket is the only transport that is implemented and it uses Starscream.
github "ricardopereira/SocketIO-Kit" "Swift2"
Then run carthage update
.
Follow the current instructions in Carthage's README for up to date installation instructions.
(work in progress)
Download the project and copy the SocketIO-Kit
folder into your project and use the Source
files. You need Runes framework to run SocketIO-Kit because it uses infix operators for monadic functions.
There is no need for import SocketIOKit
when manually installing.
- iOS 8.0+ / Mac OS X 10.10+
- Xcode 6.3 (Swift 1.2)
import SocketIOKit
// Type safety events
enum AppEvents: String, Printable {
case ChatMessage = "chat message"
case GetImage = "getimage"
case Image = "image"
var description: String {
return self.rawValue
}
}
// Mutable
var socket: SocketIO<AppEvents>!
socket = SocketIO(url: "http://localhost:8000/")
// or
// Immutable
let socket = SocketIO<AppEvents>(url: "http://localhost:8000/")
socket.on(.ConnectError) {
switch $0 {
case .Failure(let error):
println(error)
default:
break
}
}.on(.Connected) { (arg: SocketIOArg) -> () in
println("Connected")
socket.emit(.ChatMessage, withMessage: "I'm iOS")
}.on(.Disconnected) {
switch $0 {
case .Message(let message):
println("Disconnected with no error")
case .Failure(let error):
println("Disconnected with error: \(error)")
default:
break
}
}
socket.on(.ChatMessage) {
switch $0 {
case .Message(let message):
println("Remote: \(message)")
default:
println("Not supported")
}
}
socket.connect()
Options
socket = SocketIO(url: "http://localhost:8001/", withOptions: SocketIOOptions().namespace("/gallery"))
Retrieving an image
// NodeJS Server
var io = require('socket.io')(http)
io.on('connection', function(socket) {
socket.on('getimage', function(msg) {
// Image
fs.readFile(__dirname + '/image.png', function(err, buf){
// It's possible to embed binary data within arbitrarily-complex objects
socket.emit('image', { image: true, buffer: buf.toString('base64') });
});
});
});
// SocketIO-Kit Client
socket.on(.Image) {
switch $0 {
case .JSON(let json):
if let image = json["buffer"] as? String >>- SocketIOUtilities.base64EncodedStringToUIImage {
println(image)
}
default:
println("Not supported")
}
}
Working with structs
struct ImageInfo: SocketIOObject {
let buffer: String
init(buffer: String) {
self.buffer = buffer
}
init(dict: NSDictionary) {
self.init(buffer: dict["buffer"] as! String) //Force casts should be avoided!
}
var asDictionary: NSDictionary {
return ["buffer": buffer]
}
}
// Example using ImageInfo
socket.on(.Image) {
switch $0 {
case .JSON(let json):
let imageInfo = ImageInfo(dict: json) //<---
if let image = imageInfo.buffer >>- SocketIOUtilities.base64EncodedStringToUIImage {
println(image)
}
default:
println("Not supported")
}
}
Working with classes
class Person: SocketIOObject {
let name: String
init(name: String) {
self.name = name
}
convenience required init(dict: NSDictionary) {
self.init(name: dict["name"] as! String) //Force casts should be avoided!
}
var asDictionary: NSDictionary {
return ["name": name]
}
}
// Example sending John instance
let john = Person(name: "John")
socket.emit(.Login, withObject: john)
- Reconnect automatically
- Complete documentation
- Test on iOS 7.0+ and Mac OS X 10.9+
- BDD Tests
You can opt into seeing messages by supplying the DEBUG flag. Just add -D DEBUG
in Build Settings > Swift Compiler - Custom Flags: Other Swift Flags
.
See the CONTRIBUTING document. Thank you, contributors!
Ricardo Pereira, @ricardopereiraw
It is free software and may be redistributed under the terms specified in the LICENSE file.