0

I'm trying to build a handshake between an iOS app and a Socketio server based off of this tutorial: Teehan+Lax

The issue I'm having is that a connection is never opened properly, which I think is because the Handshake + token aren't being generated properly:

func initHandshake() {
    let time:NSTimeInterval = NSDate().timeIntervalSince1970 * 1000

    println(time)

    var timeString = "\(time)"

    var formattedTime = timeString.stringByReplacingOccurrencesOfString(".", withString: "-", options: NSStringCompareOptions.LiteralSearch)

    println(formattedTime)

    var endpoint = "http://\(server)/socket.io/1?t=\(formattedTime)"

    println(endpoint)

    var handshakeTask:NSURLSessionTask = session!.dataTaskWithURL(NSURL(fileURLWithPath: endpoint)!, completionHandler: { (data:NSData!, response:NSURLResponse!, error:NSError!) in
        if !(error != nil) {
            let stringData:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!
            let handshakeToken:NSString = stringData.componentsSeparatedByString(":")[0] as NSString
            println("HANDSHAKE \(handshakeToken)")

            self.socketConnect(handshakeToken)
        }
        if ((error) != nil) {
            println("Didn't connect. Why?")
        }
    })
    handshakeTask.resume()
}

You can see a few attempts I made in the code to match the endpoint URL with the string that Socketio returns, but no dice. It still doesn't connect.

I haven't written any Swift before so I'm fairly lost about what this code is actually trying to grab:

var handshakeTask:NSURLSessionTask = session!.dataTaskWithURL(NSURL(fileURLWithPath: endpoint)!, completionHandler: { (data:NSData!, response:NSURLResponse!, error:NSError!) in

Anyone have any ideas?

scoob
  • 367
  • 2
  • 14
  • I'd recommend using a library on your iOS app which will abstract away the lower level details of the socket.io handshake. I've used https://github.com/MegaBits/SIOSocket – influxd Jan 28 '15 at 16:09

1 Answers1

0

I ended up going with @influxd's suggestion below and using SIOSocket. What really ended up helping me out was a comment buried long ago in the issues here to get started writing the code once I had imported SIOSocket into my project:

import UIKit

let host = "http://localhost:3000/"

let username = "swiftbot"

let message = "Hello World"

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.connectToHost()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func connectToHost() {
    SIOSocket.socketWithHost(host, reconnectAutomatically: true, attemptLimit: 0, withDelay: 1, maximumDelay: 5, timeout: 20, response: {socket in
        socket.onConnect = {
            println("Connected to \(host)")
            socket.emit("add user", args: [username])
        }
        socket.on("login", callback: {(AnyObject data) -> Void in
            println(["login": data])
            socket.emit("new message", args: [message])
        })
        socket.onDisconnect = {
            println("Disconnected from \(host)")
        }
    })
}
}
scoob
  • 367
  • 2
  • 14