socket更换为GCDSocket
This commit is contained in:
parent
26a52f68df
commit
7a8e8e46d9
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AsyncUdpSocket.h"
|
||||
#import "GCDAsyncUdpSocket.h"
|
||||
#import "IFishUDPHelperBackMsgModel.h"
|
||||
|
||||
@protocol IFishHotpotUDPHelperDelegate<NSObject>
|
||||
|
|
@ -18,9 +19,9 @@
|
|||
|
||||
@end
|
||||
|
||||
@interface IFishHotpotUDPHelper : NSObject <AsyncUdpSocketDelegate>
|
||||
@interface IFishHotpotUDPHelper : NSObject <AsyncUdpSocketDelegate,GCDAsyncUdpSocketDelegate>
|
||||
|
||||
@property (nonatomic, strong) AsyncUdpSocket *clientSocket;
|
||||
@property (nonatomic, strong) GCDAsyncUdpSocket *clientSocket;
|
||||
|
||||
+ (IFishHotpotUDPHelper *)sharedInstance;
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
self.clientSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
|
||||
self.bindok=NO;
|
||||
[self bind];
|
||||
}
|
||||
|
|
@ -110,7 +110,8 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
|
||||
UInt16 port = 333;
|
||||
self.tag = 2;
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
// [self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
[self.clientSocket beginReceiving:nil];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
|
||||
}
|
||||
|
|
@ -131,7 +132,8 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
|
||||
UInt16 port = 333;
|
||||
self.tag = 1;
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
//[self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
[self.clientSocket beginReceiving:nil];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
|
||||
}
|
||||
|
|
@ -148,13 +150,15 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
|
||||
UInt16 port = 333;
|
||||
self.tag = 0;
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
// [self.clientSocket receiveWithTimeout:-1 tag:self.tag];
|
||||
[self.clientSocket beginReceiving:nil];
|
||||
|
||||
BOOL send= [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
|
||||
if (!send)
|
||||
{
|
||||
[self postmsg:@"发送失败"];
|
||||
}
|
||||
//BOOL send=
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
|
||||
// if (!send)
|
||||
// {
|
||||
// [self postmsg:@"发送失败"];
|
||||
// }
|
||||
}
|
||||
- (void)closeComplete:(void (^)(void))block
|
||||
{
|
||||
|
|
@ -184,6 +188,24 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
}
|
||||
return YES;
|
||||
|
||||
}
|
||||
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
|
||||
{
|
||||
[self.loopTimer invalidate];
|
||||
self.loopTimer = nil;
|
||||
|
||||
NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||
IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data];
|
||||
if ([model.result isEqualToString:@"01"]) {
|
||||
if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess:)]) {
|
||||
[self.delegate udpHelperCommandExecutedSuccess:model];
|
||||
}
|
||||
} else {
|
||||
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
|
||||
[app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]];
|
||||
}
|
||||
// return YES;
|
||||
|
||||
}
|
||||
|
||||
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
|
||||
|
|
@ -193,6 +215,14 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
self.closeBlock();
|
||||
}
|
||||
}
|
||||
-(void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
|
||||
{
|
||||
[self postmsg:@"连接关闭"];
|
||||
self.bindok=NO;
|
||||
if (self.closeBlock) {
|
||||
self.closeBlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
|
||||
|
|
@ -201,6 +231,13 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
[self postmsg:@"指令发送"];
|
||||
|
||||
}
|
||||
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag
|
||||
{
|
||||
|
||||
NSLog(@"didSendData");
|
||||
[self postmsg:@"指令发送"];
|
||||
|
||||
}
|
||||
|
||||
|
||||
-(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
|
||||
|
|
@ -208,6 +245,12 @@ static IFishHotpotUDPHelper *sharedInstace = nil;
|
|||
NSLog(@"dueToError%@",error);
|
||||
[self postmsg:[NSString stringWithFormat:@"Socket连接失败:%@",error.localizedDescription]];
|
||||
}
|
||||
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
|
||||
{
|
||||
|
||||
NSLog(@"dueToError%@",error);
|
||||
[self postmsg:[NSString stringWithFormat:@"Socket连接失败:%@",error.localizedDescription]];
|
||||
}
|
||||
|
||||
|
||||
-(void)dealloc{
|
||||
|
|
|
|||
Loading…
Reference in New Issue