Socket.IO-Client-Swift
Socket.IO-client for Swift. Supports ws/wss connections and binary. For socket.io 1.0+
Installation
- Requires linking SocketRocket against your xcode project. (Be sure to link the frameworks required by SocketRocket)
 - Create a bridging header for SocketRocket
 - Copy the SwiftIO folder into your xcode project
 
API
Constructor
init(socketURL: String, opts[String: AnyObject]? = nil) - Constructs a new client for the given URL. opts can be omitted (will use default values.)
Methods
socket.on(name:String, callback:((data:AnyObject?) -> Void))- Adds a handler for an event.socket.onMultipleItems(name:String, callback:((data:AnyObject...) -> Void))- Adds a handler for an event that can have multiple items. Items are stored in an array.socket.emit(event:String, args:AnyObject...)- Sends a message. Can send multiple args.socket.connect()- Establishes a connection to the server. A "connect" event is fired upon successful connection.socket.close()- Closes the socket. Once a socket is closed it should not be reopened.
Events
connect- Emitted when on a successful connection.disconnect- Emitted when the connection is closed.reconnect- Emitted when the connection is starting to reconnect.reconnectAttempt- Emitted when attempting to reconnect.
// opts can be omitted, will use default values
let socket = SocketIOClient(socketURL: "https://localhost:8080", opts: [
    "reconnects": true, // default true
    "reconnectAttempts": 5, // default -1 (infinite tries)
    "reconnectWait": 5 // default 10
])
// Socket Events
socket.on("connect") {data in
    println("socket connected")
    
    // Sending messages
    socket.emit("testEcho")
    socket.emit("testObject", [
        "data": true
        ])
    // Sending multiple items per message
    socket.emit("multTest", [1], 1.4, 1, "true",
        true, ["test": "foo"], "bar")
}
socket.on("disconnect") {data in
    if let reason = data as? String {
        println("Socket disconnected: \(reason)")
    }
}
socket.on("reconnect") {data in
    if let reason = data as? String {
        println("Socket reconnecting: \(reason)")
    }
}
socket.on("reconnectAttempt") {data in
    if let triesLeft = data as? Int {
        println(triesLeft)
    }
}
// End Socket Events
socket.on("jsonTest") {data in
    if let json = data as? NSDictionary {
       println(json["test"]!) // foo bar
    }
}
// Messages that have multiple items are passed
// by an array
socket.onMultipleItems("multipleItems") {data in
    if let str = data[0] as? String {
        println(str)
    }
    if let arr = data[1] as? [Int] {
        println(arr)
    }
    if let obj = data[4] as? NSDictionary {
        println(obj["test"])
    }
}
        
// Recieving binary
socket.on("dataTest") {data in
    if let data = data as? NSData {
        println("data is binary")
    }
}
socket.on("objectDataTest") {data in
    if let dict = data as? NSDictionary {
        if let data = dict["data"] as? NSData {
            let string = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("Got data: \(string!)")
        }
    }
}
// Connecting
socket.connect()
// Sending binary
socket.emit("testData", [
        "data": "Hello World".dataUsingEncoding(NSUTF8StringEncoding,
            allowLossyConversion: false)!,
        "test": true])
License
MIT
Description
				
					Languages
				
				
								
								
									Swift
								
								99.3%
							
						
							
								
								
									Ruby
								
								0.5%
							
						
							
								
								
									Objective-C
								
								0.2%