AsyncSocket subscriber calls do not call in fast class

GCDAsyncSocket delegates do not call swift in the class, but work fine in the UIViewController class. Below is my custom class code, in this class the connection function will start connecting sockets, and the delegate is never called. I search on the net as well as in the GCDAsyncSocket github repo, but have not succeeded.

class RXSocket: NSObject,GCDAsyncSocketDelegate {

func connect() {

    let asyncSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())

    do {
        try asyncSocket?.connectToHost("www.google.com", onPort: 80)
    } catch _ as NSError {
    }

}


//MARK:- ASyncSocket Delegate

func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
    print("didConnectToHost")
}

func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
    print("didReceiveData")
}

func socketDidDisconnect(sock: GCDAsyncSocket!, withError err: NSError!) {
    print("socketDidDisconnect")
}

}

and, in my opinion, the controller class

    let rxSocket = RXSocket()
    rxSocket.connect()
+4
source share
2 answers

I experienced the same problem and solved it.

Your sample code is fine.

The reason the callback does not work, most likely your instance of the object (for example, "rxSocket") does not remain with the connect () call

,

func testMyConnection() { 
    let rxSocket = RXSocket()
    rxSocket.connect()
}

rxSocket , rxSocket .

, rxSocket , connect(), , GDCAsyncSocket.

0

:

class RXSocket: NSObject,GCDAsyncSocketDelegate {
var asyncSocket: GCDAsyncSocket!
func connect() {

 self.asyncSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())

do {
    try asyncSocket?.connectToHost("www.google.com", onPort: 80)
} catch _ as NSError {
}

}
}
-1

All Articles