I'm trying to write an app to send/receive data by means of the CocoaAsyncSocket library.
In the first version of the App, the socket is created/initialized in the View Controller where I also put the delegate methods that are correctly invoked:
WakmanFirstViewController.m
#import "WakmanFirstViewController.h"
#import "GCDAsyncSocket.h"
@implementation WakmanFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
if (![asyncSocket connectToHost:@"192.168.1.13" onPort:2000 error:&error]) {
NSLog(@"Unable to connect to due to invalid configuration: %@",error);
}
NSData *requestData = [@"C" dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
[asyncSocket readDataWithTimeout:1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(@"socket:didConnectToHost:%@ port:%hu", host, port);
}
... (other delegate methods)
Now I'm trying to remove the creation of the socket from the ViewController and put it in a Singleton class so that I can use the same connection also from other views.
To do this I have created a new class (SocketConnection) where I moved also the delegate methods:
wakmanSocketConnection.h
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"
@interface SocketConnection : NSObject {
}
+ (GCDAsyncSocket *)getInstance;
@end
wakmanSocketConnection.m
#import "SocketConnection.h"
static GCDAsyncSocket *socket;
@implementation SocketConnection
+ (GCDAsyncSocket *)getInstance
{
@synchronized(self) {
if (socket == nil) {
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
if (![socket isConnected]) {
NSError *error = nil;
if (![socket connectToHost:@"192.168.1.13" onPort:2000 error:&error]){
NSLog(@"Error connecting: %@", error);
}
}
}
return socket;
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(@"socket connected");
}
... (other delegate methods)
I then modified the Viewcontroller:
WakmanFirstViewController.h
#import <UIKit/UIKit.h>
#import "SocketConnection.h"
@class GCDAsyncSocket;
@interface WakmanFirstViewController : UIViewController {
GCDAsyncSocket *socket;
}
@end
WakmanFirstViewController.m
#import "WakmanFirstViewController.h"
@implementation WakmanFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
socket = [SocketConnection getInstance];
NSData *requestData = [@"C" dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
[asyncSocket readDataWithTimeout:1 tag:0];
}
The connection is established but the problem is that the delegate methods are not invoked.
In wakmanSocketConnection.m I set the delegate to self so it should refer to the class where I copied the methods.
Can someone help me in finding the problem ?
Thanks,
Corrado