SocketIOKit 2.0.1

SocketIOKit 2.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2015
SPMSupports SPM

Maintained by Ricardo Pereira.



 
Depends on:
Runes= 2.0.0
SwiftWebSocket= 0.1.18
 

SocketIO-Kit Logo 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 SwiftWebSocket.

Installation

Manually

Download the project and copy the SocketIO-Kit folder into your project and use the Source files. You need Runes v2.0.0 (Swift 1.2) framework to run SocketIO-Kit because it uses infix operators for monadic functions and you need SwiftWebSocket v0.1.18 (Swift 1.2).

There is no need for import SocketIOKit when manually installing.

Requirements

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 6.3 (Swift 1.2)

⚠️ Swift 2.0 version, please use branch Swift2.

Usage

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(.TransportError) {
    switch $0 {
    case .Failure(let error):
        println("WebSocket error: \(error)")
    default:
        break
    }
}.on(.Connected) { result in
    println("Connected")
    socket.emit(.ChatMessage, withMessage: "I'm iOS")
}.on(.Disconnected) { result in
    switch result {
    case .Message(let reason):
        println("Disconnected: \(reason)")
    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"))

Examples

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)

Features

To implement
  • [ ] Reconnect automatically
  • [ ] Complete documentation
  • [ ] Test on iOS 7.0+ and Mac OS X 10.9+
  • [ ] BDD Tests

Debugging

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.

Contributing

See the CONTRIBUTING document. Thank you, contributors!

Author

Ricardo Pereira, @ricardopereiraw

License

It is free software and may be redistributed under the terms specified in the LICENSE file.