优化连接过程

This commit is contained in:
xuemh
2018-08-26 23:39:52 +08:00
parent ff26073344
commit d0e290bea6
3 changed files with 67 additions and 101 deletions
@@ -21,8 +21,6 @@
NSString *_pwd;
}
@property (nonatomic, assign) BOOL backMessageReceived;
@end
@@ -35,60 +33,55 @@
dispatch_once(&onceToken, ^{
sharedInstace = [[self alloc] init];
});
return sharedInstace;
}
- (instancetype)init {
if (self = [super init]) {
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
UInt16 bindPort = 9954;
NSError *error;
[self.clientSocket enableBroadcast:YES error:&error];
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
[self.clientSocket receiveWithTimeout:-1 tag:0];
}
return self;
}
- (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd {
_ssidName = ssidName;
_pwd = pwd;
[self postmsg:@"命令已发出"];
self.backMessageReceived = NO;
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendConnectHotspotSoketdata) userInfo:nil repeats:YES];
[self sendConnectHotspotSoketdata];
}
- (void)broadCastRestCommand {
[self postmsg:@"命令已发出"];
self.backMessageReceived = NO;
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestSocketData) userInfo:nil repeats:YES];
[self sendRestSocketData];
}
- (void)sendRestSocketData {
NSString* hostS = @"192.168.4.1";
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//120.55.190.56 服务器IP 指令中用
ResetDeviceModel *model = [ResetDeviceModel new];
NSString *request=[NSString stringWithFormat:@"%@", model.description];
NSData *data=[dataContorl stringToHexData:request];
UInt16 port = 333;
UInt16 bindPort = 9954;
NSError *error;
[self.clientSocket enableBroadcast:YES error:&error];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
[self.clientSocket receiveWithTimeout:-1 tag:0];
}
- (void)broadCastRestartCommand {
[self postmsg:@"命令已发出"];
self.backMessageReceived = NO;
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestartSocketData) userInfo:nil repeats:YES];
[self sendRestSocketData];
}
- (void)sendRestartSocketData {
NSString* hostS = @"192.168.4.1";
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//120.55.190.56 服务器IP 指令中用
RestartDeviceModel *model = [RestartDeviceModel new];
NSString *request=[NSString stringWithFormat:@"%@", model.description];
@@ -96,24 +89,13 @@
NSData *data=[dataContorl stringToHexData:request];
UInt16 port = 333;
UInt16 bindPort = 9954;
NSError *error;
[self.clientSocket enableBroadcast:YES error:&error];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
[self.clientSocket receiveWithTimeout:-1 tag:0];
}
-(void)sendConnectHotspotSoketdata{
NSString* hostS = @"192.168.4.1";
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//120.55.190.56 服务器IP 指令中用
ConnectHotspotModel *model = [ConnectHotspotModel new];
model.ssidName = _ssidName;
@@ -123,27 +105,14 @@
NSData *data=[dataContorl stringToHexData:request];
UInt16 port = 333;
UInt16 bindPort = 9954;
NSError *error;
[self.clientSocket enableBroadcast:YES error:&error];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
[self.clientSocket receiveWithTimeout:-1 tag:0];
}
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
if (self.backMessageReceived) {
return YES;
}
self.backMessageReceived = YES;
[self.loopTimer invalidate];
NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data];
if ([model.result isEqualToString:@"01"]) {
@@ -154,15 +123,12 @@
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
[app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]];
}
[self.clientSocket close];
[self.loopTimer invalidate];
return YES;
}
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
[self.clientSocket close];
[self.loopTimer invalidate];
NSLog(@"关闭");
}
@@ -179,7 +145,6 @@
NSLog(@"dueToError%@",error);
[self postmsg:error.localizedDescription];
[self.clientSocket close];
[self.loopTimer invalidate];
}