IOS 在 NSObject 中实现时未调用 GCDAsyncSocket didReadData class
IOS GCDAsyncSocket didReadData not called when implement in NSObject class
我在 NSObject class 中实现 GCDAsyncSocket 委托,然后在 viewController 中调用它。
当 运行 它时,我发现 'didConnectToHost' 和 'didWriteDataWithTag' 被调用,但我没有看到 'didReadData' 被调用。这是我的代码:
SocketUtils.h
@interface SocketUtils : NSObject <GCDAsyncSocketDelegate>
@property (nonatomic) void (^SocketCallback)(id data);
@property (nonatomic) NSString *message;
@property (nonatomic) UIViewController *vc;
+ (SocketUtils *) createSocket:(NSString *)message inViewController:(UIViewController *)vc;
- (void) connectToSocket;
@end
SocketUtils.m
@implementation SocketUtils{
GCDAsyncSocket *socket;
}
+ (SocketUtils *) createSocket:(NSString *)message inViewController:(UIViewController *)vc{
SocketUtils *this = [[self alloc] init];
this.message = message;
[this connectToSocket];
return this;
}
- (void)connectToSocket{
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:SOCKET_IP onPort:[SOCKET_PORT intValue] error:&err])
{
NSLog(@"err: %@", [err localizedDescription]);
if(err){
[Utils showErrorDialog:[err localizedDescription] atViewController:_vc];
}
return;
}
}
#pragma mark - Socket delegate
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Cool,didConnectToHost: %@", host);
[socket writeData:[_message dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[socket readDataWithTimeout:-1 tag:0];
NSLog(@"didWriteDataWithTag: %ld", tag);
}
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"didReadData : %@", data);
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
_SocketCallback(msg);
}
- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock{
NSLog(@"socketDidCloseReadStream");
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
NSLog(@"disconnected to socket: %@", err);
[Utils showErrorDialog:[err localizedDescription] atViewController:_vc];
}
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);
}
然后在 viewController 我调用了:
socketUtils = [SocketUtils createSocket:@"GET_BOARD_CODE" inViewController:self];
socketUtils.SocketCallback = ^(id data) {
doSth();
};
我预测的第一件事是破坏这个 class,但我试图保持所有 @属性 = 强大,但没有用。
有人能帮忙吗?
找了很多次,才发现是忘记在留言后面加上“\r\n”了。好的,只需将 1 行代码更改为:
this.message = [message stringByAppendingString:@"\r\n"];
它按预期工作。
我在 NSObject class 中实现 GCDAsyncSocket 委托,然后在 viewController 中调用它。 当 运行 它时,我发现 'didConnectToHost' 和 'didWriteDataWithTag' 被调用,但我没有看到 'didReadData' 被调用。这是我的代码:
SocketUtils.h
@interface SocketUtils : NSObject <GCDAsyncSocketDelegate>
@property (nonatomic) void (^SocketCallback)(id data);
@property (nonatomic) NSString *message;
@property (nonatomic) UIViewController *vc;
+ (SocketUtils *) createSocket:(NSString *)message inViewController:(UIViewController *)vc;
- (void) connectToSocket;
@end
SocketUtils.m
@implementation SocketUtils{
GCDAsyncSocket *socket;
}
+ (SocketUtils *) createSocket:(NSString *)message inViewController:(UIViewController *)vc{
SocketUtils *this = [[self alloc] init];
this.message = message;
[this connectToSocket];
return this;
}
- (void)connectToSocket{
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:SOCKET_IP onPort:[SOCKET_PORT intValue] error:&err])
{
NSLog(@"err: %@", [err localizedDescription]);
if(err){
[Utils showErrorDialog:[err localizedDescription] atViewController:_vc];
}
return;
}
}
#pragma mark - Socket delegate
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Cool,didConnectToHost: %@", host);
[socket writeData:[_message dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[socket readDataWithTimeout:-1 tag:0];
NSLog(@"didWriteDataWithTag: %ld", tag);
}
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"didReadData : %@", data);
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
_SocketCallback(msg);
}
- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock{
NSLog(@"socketDidCloseReadStream");
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
NSLog(@"disconnected to socket: %@", err);
[Utils showErrorDialog:[err localizedDescription] atViewController:_vc];
}
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);
}
然后在 viewController 我调用了:
socketUtils = [SocketUtils createSocket:@"GET_BOARD_CODE" inViewController:self];
socketUtils.SocketCallback = ^(id data) {
doSth();
};
我预测的第一件事是破坏这个 class,但我试图保持所有 @属性 = 强大,但没有用。
有人能帮忙吗?
找了很多次,才发现是忘记在留言后面加上“\r\n”了。好的,只需将 1 行代码更改为:
this.message = [message stringByAppendingString:@"\r\n"];
它按预期工作。