GCDAsyncSocket delegates not being called in swift class, but works very well in UIViewController class. Below is my custom class code, in this class connect function will start socket connection and it's delegate will never called. I search on net and also on GCDAsyncSocket github repo but no success.
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 view controller class
let rxSocket = RXSocket()
rxSocket.connect()