nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

unable to fire functions in socketioclint.swift #8

Closed elcocui closed 9 years ago

elcocui commented 9 years ago

Hi, Im trying to make an IOS app to connect to arduino via a wifly shield. I got the shield listening in 192.168.1.111:8000. Im able to connect and disconnect to the wifly board, but I'm not able to send data. On further study, i found that the code is not firing func websocketdidopen() on opening the port (same with closing).The events including socket.on("connect") do not respond after connection is created. Please help me out. code:

import UIKit import Foundation class ViewController: UIViewController { var connectionStatus: Bool = false let socket = SocketIOClient(socketURL: "192.168.1.111:8000", opts: nil)

@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var testButton: UIButton!
@IBOutlet weak var status: UILabel!
@IBAction func connectButton(sender: AnyObject) {
        socket.connect()

            self.status.text = "Connected"
            self.connectButton.hidden = true
            self.testButton.hidden = false

    disconnectButton.hidden = false

    socket.on("connect") {data in
        println("socket connected")

           }
}

@IBAction func sendTestMessage(sender: AnyObject) {
 socket.emit("echo")
    socket.sendPing()

    println("echo")

}

@IBOutlet weak var disconnectButton: UIButton!
@IBAction func disconnectButtonPressed(sender: AnyObject) {
    disconnectButton.hidden = true
    connectButton.hidden = false
    testButton.hidden = true
    socket.close()
    socket.on("disconnect") {data in
        if let reason = data as? String {
            println("Socket disconnected: \(reason)")
        }
    }
}

//functions

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    testButton.hidden = true
    disconnectButton.hidden = true
}

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

}

nuclearace commented 9 years ago

In your code it appears that you are calling connect before you actually add the handlers. It's more correct to add your handlers before you call connect. Because once you call connect there's no guarantee that your handler will be added in time to respond to the events.

nuclearace commented 9 years ago

Also, there really isn't any reason to call sendPing yourself, the socket will set up a timer to call it.

elcocui commented 9 years ago

thank you. I will try the suggestions and let you know how it works

nuclearace commented 9 years ago

Any luck?

elcocui commented 9 years ago

Did not try it yet sir. I have been out for a conference. Will let you know how it goes asap. Thanks for asking.

elcocui commented 9 years ago

I tried the suggestions, but unfortunately, the event handlers are not being initiated. I'm suspecting that I made a mistake while setting up the Xcode project, but I have no idea what it is because there seem to be no errors or warnings. It would be really helpful if you could add an example app/project using swift.io sockets. Sorry for the trouble. You have done an incredible job with the code.

nuclearace commented 9 years ago

https://github.com/nuclearace/CytubeChatiOS/tree/1.1/CytubeChat1.0 Is what I made the client for.

nuclearace commented 9 years ago

Specifically check out: https://github.com/nuclearace/CytubeChatiOS/blob/1.1/CytubeChat1.0/CytubeRoom.swift https://github.com/nuclearace/CytubeChatiOS/blob/1.1/CytubeChat1.0/CytubeRoom.swift#L47 https://github.com/nuclearace/CytubeChatiOS/blob/1.1/CytubeChat1.0/CytubeRoom.swift#L297 https://github.com/nuclearace/CytubeChatiOS/blob/1.1/CytubeChat1.0/CytubeRoom.swift#L308

open() just calls connect()

elcocui commented 9 years ago

thanks for the quick reply. Checking the code right now.

nuclearace commented 9 years ago

Also just as a sanity check, you are trying to connect to a socket.io server and not just a WebSockets server correct? This is only for socket.io. However if you're just trying to connect to a WebSockets server you'd probably be best using SocketRocket. You could use SocketIOClient.swift as an example of a delegate class.

You'd need to change https://github.com/nuclearace/Socket.IO-Client-Swift/blob/master/SwiftIO/SocketIOClient.swift#L100 to wss://\(self.socketURL) or ws://\(self.socketURL). And then write your own parser for webSocket(webSocket:SRWebSocket!, didReceiveMessage message:AnyObject?), and handle the other delegate methods accordingly.

criticalmatter commented 9 years ago

elcocui - Put a break point in SRWebSocket here:

https://github.com/square/SocketRocket/blob/master/SocketRocket/SRWebSocket.m#L476

Check the value for the delegate of SRWebSocket. Is it nil? It sounds to me as if your Socket-IO-Client object is probably disappearing. You might consider implementing a deinit() in whatever class you instantiate your Socket-IO-Client in (or in Socket-IO-Client itself) to see when / where your SRWebSocket delegate is going away. I'm guessing that you will discover that it's not persisting!