gpt4 book ai didi

ios - 未调用 AsyncSocket 委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 20:26:27 26 4
gpt4 key购买 nike

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

在应用程序的第一个版本中,套接字是在 View Controller 中创建/初始化的,我还在其中放置了正确调用的委托(delegate)方法:

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 中删除套接字的创建并将其放入单例类中,以便我也可以在其他 View 中使用相同的连接。

为此,我创建了一个新类 (SocketConnection),并在其中移动了委托(delegate)方法:

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];
}

连接已建立,但问题是未调用委托(delegate)方法。

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

谁能帮我找出问题所在?

谢谢,科拉多

最佳答案

问题是 didReadData 委托(delegate)方法只被调用一次,你必须通过调用继续监听套接字:

[_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];
}
});
}

初始化方法:

- (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];
}
}

关于ios - 未调用 AsyncSocket 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com