添加IFishHotpotUDPHelper
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// IfishUDPBroadCastHelper.m
|
||||
// Ifish
|
||||
//
|
||||
// Created by imac on 16/9/12.
|
||||
// Copyright © 2016年 lianxiang. All rights reserved.
|
||||
//
|
||||
|
||||
#import "IFishHotpotUDPHelper.h"
|
||||
#import "LxGetCurrentIp.h"
|
||||
#import "AppDelegate.h"
|
||||
|
||||
@interface IFishHotpotUDPHelper ()
|
||||
{
|
||||
NSString * _ssidName;
|
||||
NSString *_pwd;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
@implementation IFishHotpotUDPHelper
|
||||
|
||||
+ (IFishHotpotUDPHelper *)sharedInstance{
|
||||
|
||||
static IFishHotpotUDPHelper *sharedInstace = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
sharedInstace = [[self alloc] init];
|
||||
});
|
||||
|
||||
return sharedInstace;
|
||||
}
|
||||
|
||||
- (void)broadCaseUDPWith:(NSString *)ssidName pwd:(NSString *)pwd {
|
||||
_ssidName = ssidName;
|
||||
_pwd = pwd;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendLoopSoketdata) userInfo:nil repeats:YES];
|
||||
}
|
||||
|
||||
-(void)sendLoopSoketdata{
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
NSString *request=@"0007000000000000000000000000273132302e35352e3139302e353600000000000000bbbb0000";
|
||||
|
||||
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
|
||||
{
|
||||
NSString* result;
|
||||
|
||||
result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||
|
||||
NSLog(@"didReceiveData%@",result);
|
||||
NSLog(@"port%hu",port);
|
||||
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
|
||||
[app.window makeToast:@"切换成功"];
|
||||
[self.loopTimer invalidate];
|
||||
return YES;
|
||||
|
||||
}
|
||||
|
||||
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
|
||||
|
||||
[self.loopTimer invalidate];
|
||||
NSLog(@"关闭");
|
||||
}
|
||||
|
||||
|
||||
-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
|
||||
|
||||
NSLog(@"didSendData");
|
||||
[self postmsg:@"发送成功"];
|
||||
}
|
||||
|
||||
|
||||
-(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
|
||||
|
||||
NSLog(@"dueToError%@",error);
|
||||
[self postmsg:error.localizedDescription];
|
||||
[self.loopTimer invalidate];
|
||||
}
|
||||
|
||||
|
||||
-(void)dealloc{
|
||||
|
||||
[self.loopTimer invalidate];
|
||||
|
||||
}
|
||||
|
||||
- (void)postmsg:(NSString *)msg {
|
||||
if ([self.delegate respondsToSelector:@selector(udpHelperMessage:)]) {
|
||||
[self.delegate udpHelperMessage:msg];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user