首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用AsyncSocket委托

未调用AsyncSocket委托
EN

Stack Overflow用户
提问于 2012-12-06 22:42:23
回答 1查看 3.7K关注 0票数 0

我试图通过CocoaAsyncSocket库编写一个应用程序来发送/接收数据。

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

WakmanFirstViewController.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"

@interface SocketConnection : NSObject {
}

+ (GCDAsyncSocket *)getInstance;

@end

wakmanSocketConnection.m

代码语言:javascript
复制
#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)

然后我修改了视图控制器:

WakmanFirstViewController.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "SocketConnection.h"

@class GCDAsyncSocket;

@interface WakmanFirstViewController : UIViewController  {
    GCDAsyncSocket *socket; 
}

@end

WakmanFirstViewController.m

代码语言:javascript
复制
#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,因此它应该引用我复制方法的类。

有人能帮我找出问题吗?

谢谢,科拉多

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-06 22:49:41

问题是,只调用一次didReadData委托方法,您必须通过调用以下方法来继续侦听套接字:

代码语言:javascript
复制
[_socket readDataWithTimeout:-1 tag:0]; 

我的方法看起来是这样的:

代码语言:javascript
复制
- (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方法:

代码语言:javascript
复制
- (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;
}

以及连接方法:

代码语言:javascript
复制
- (void)connectToServer:(NSString *)host port:(NSInteger)port
{
    self.host = host;
    self.port = port;

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

    }
}

书写方法:

代码语言:javascript
复制
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
{
    if ([self.socket isConnected]) {
        [self.socket writeData:data withTimeout:timeout tag:tag];
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13753956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档