未调用AsyncSocket委托

时间:2012-12-06 22:42:24

标签: ios delegates cocoaasyncsocket

我正在尝试编写一个应用程序,通过CocoaAsyncSocket库发送/接收数据。

在App的第一个版本中,在View Controller中创建/初始化套接字,我还放置了正确调用的委托方法:

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)

现在我正在尝试从ViewController中删除套接字的创建并将其放在Singleton类中,以便我也可以从其他视图使用相同的连接。

为此,我创建了一个新类(SocketConnection),我还移动了委托方法:

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)

然后我修改了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];
}

建立连接但问题是没有调用委托方法。

wakmanSocketConnection.m 中,我将委托设置为self,因此它应该引用我复制方法的类。

有人可以帮助我找到问题吗?

谢谢, 的Corrado

1 个答案:

答案 0 :(得分:3)

问题是didReadData委托方法只被调用一次,你必须通过调用来保持监听套接字:

[_socket readDataWithTimeout:-1 tag:0]; 

我的方法看起来像这样:

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @autoreleasepool {
            // DO STH
            [_socket readDataWithTimeout:-1 tag:0];
        }
    });
}

Init方法:

- (id)init
{
    if (self = [super init]) 
    {
        _socketQueue = dispatch_queue_create("Socket TCP Queue", nil);
        _delegateQueue = dispatch_queue_create("Socket Delegate Queue", nil);
        _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_delegateQueue socketQueue:_socketQueue];

    }
    return self;
}

和连接方法:

- (void)connectToServer:(NSString *)host port:(NSInteger)port
{
    self.host = host;
    self.port = port;

    NSError *error = nil;
    if (![_socket connectToHost:host onPort:port error:&error])
    {

    }
}

写方法:

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
{
    if ([self.socket isConnected]) {
        [self.socket writeData:data withTimeout:timeout tag:tag];
    }
}