goofansu
goofansu

Reputation: 2277

AsyncSocket delegate method not called

I retain a socket in a singleton class like following:

SocketConnection.h

@interface SocketConnection : NSObject

+ (GCDAsyncSocket *) getInstance;

@end

SocketConnection.m

#define LOCAL_CONNECTION 1

#if LOCAL_CONNECTION
#define HOST @"localhost"
#define PORT 5678
#else
#define HOST @"foo.abc"
#define PORT 5678
#endif

static GCDAsyncSocket *socket;

@implementation SocketConnection

+ (GCDAsyncSocket *)getInstance
{
    @synchronized(self) {
        if (socket == nil) {
            dispatch_queue_t mainQueue = dispatch_get_main_queue();
            socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
        }
        if (![socket isConnected]) {

            NSString *host = HOST;
            uint16_t port = PORT;
            NSError *error = nil;

            if (![socket connectToHost:host onPort:port error:&error])
            {
                NSLog(@"Error connecting: %@", error);
            }
        }
    }

    return socket;
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"socket connected");
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
    NSLog(@"socketDidDisconnect:%p withError: %@", sock, err);
}

@end

And in a viewController:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _socket = [SocketConnection getInstance];
    }
    return self;
}

I can see that the socket is connected in my server, but there is nothing in my xcode console log. Please help to see why it cannot invoke the delegate method?

Upvotes: 0

Views: 1009

Answers (1)

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

You are initializing the socket in SocketConnection's getInstance method, at which point you set the delegate to self. self refers to the SocketConnection instance, not your view controller. Either initialize the socket in the view controller (at which point it's not a singleton anymore), or create a delegate property on SocketConnection and pass delegate methods on to SocketConnection's delegate. Personally, I do the latter, but I send out notifications as opposed to delegate messages.

Upvotes: 0

Related Questions