#import <Foundation/Foundation.h>
#import "AsyncSocket.h"
#import <UIKit/UIKit.h>
enum{
SocketOfflineByServer,// 服务器掉线,默认为0
SocketOfflineByUser, // 用户主动cut
};
@class receiveImage;
@protocol receiveDelegate <NSObject,UIAlertViewDelegate>
- (void)receiver:(receiveImage *)receiveimage didReceiveData:(NSDictionary *)dicDataSource num:(NSInteger)i;
@end
@interface receiveImage :NSObject<AsyncSocketDelegate>
{
AsyncSocket *receiveSocket;
NSMutableData* respondData;
NSString *someProperty;
}
@property(nonatomic)UInt16 port;
@property(nonatomic,weak)id<receiveDelegate> delegate;
@property(nonatomic,strong)NSMutableArray *at;
@property(nonatomic,assign)int userData;
@property(nonatomic,strong)NSMutableDictionary* stus;
-(id)initWithPort:(UInt16) port;
//-(void)listen;
-(void)disConnect;
-(BOOL)connectToHost;
//发送数据
-(void)sendshuju;
@end
#import "receiveImage.h"
#import <UIKit/UIKit.h>
#define ScoketFormat @"WH\t&Tea\t&%@\t&END\t\n"
@implementation receiveImage
@synthesize port=_port;
@synthesize delegate=_delegate;
@synthesize at=_at;
-(id)initWithPort:(UInt16)port{
self=[superinit];
if (self) {
receiveSocket=[[AsyncSocketalloc] initWithDelegate:self];
self.stus = [[NSMutableDictionaryalloc] initWithCapacity:0];
}
return self;
}
-(BOOL)connectToHost{
NSError* error;
NSUserDefaults* def = [NSUserDefaultsstandardUserDefaults];
NSString* ips = [def objectForKey:@"ipvnum"];
NSInteger duankou = [[def objectForKey:@"wzkport"] integerValue];
BOOL bol = [receiveSocketconnectToHost:ips onPort:duankou withTimeout:-1error:&error];
if (bol == YES) {
[receiveSocketreadDataWithTimeout:-1tag:0];
}
else
{
[selfduankaitixing];
}
return bol;
}
//-(void)listen{
// NSLog(@"listen");
// if (![receiveSocket isConnected]) {
// NSError *error=NULL;
// NSLog(@"AAAA");
// if (![receiveSocket acceptOnPort:self.port error:&error]) {
// NSLog(@"%@",error);
// }
// }
//}
/*
* 协议规则
* 数据(目前是字符串)是一条一条的过来,再将字符串转化到一个一个的字典(对应的每一个学生),再用一个大字典存放。这个大字典就是存放每个学生的信息,用ipad的唯一表识做为key值,这样同一个学生就会实时更新
*/
-(void)didRead:(NSString *)netString
{
// NSLog(@"didRead %@",netString);
NSArray * dataarr = [netString componentsSeparatedByString:@"\t&"];
if (dataarr.count>=8) {
if ([@"WH"isEqualToString:dataarr[0]]) {
if ([@"Ctr"isEqualToString:dataarr[1]]) {
if ([@"StuInfos"isEqualToString:dataarr[2]]) {
NSString *stu_uuid = dataarr[3];
NSString *stu_info = dataarr[4];
NSString *stu_game = dataarr[5];
NSString *stu_task = dataarr[6];
NSString *stu_type = dataarr[7];
NSDictionary *stu_infos = @{
@"stu_uuid":stu_uuid,
@"stu_info":stu_info,
@"stu_game":stu_game,
@"stu_task":stu_task,
@"stu_type":stu_type};
[self.stussetObject:stu_infos forKey:stu_uuid];
}
}
}
}
if ([self.delegaterespondsToSelector:@selector(receiver:didReceiveData:num:)]) {
[self.delegatereceiver:nildidReceiveData:self.stusnum:0];
}
}
-(void)sendshuju
{
NSString *paduuid = [UIDevicecurrentDevice].identifierForVendor.UUIDString ;
NSString *padlogin = [NSStringstringWithFormat:@"login\t&%@",paduuid];
[self sendString:padlogin];
}
-(void)sendString:(NSString *)str
{
NSString *dabaoString = [NSStringstringWithFormat:ScoketFormat,[strstringByReplacingOccurrencesOfString:@"\n"withString:@""]];
NSData *dataStream = [dabaoString dataUsingEncoding:NSUTF8StringEncoding];
[receiveSocket writeData:dataStream withTimeout:-1tag:1];
}
//
-(void)duankaitixing
{
// UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器已经断开,请重新链接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
// alert.delegate = self;
// [alert show];
[selfconnectToHost];
}
-(void)disConnect{
[receiveSocketdisconnect];
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
BOOL bol = [sock isConnected];
NSLog(@"%d",bol);
if(bol == NO){
if (self.userData == SocketOfflineByUser) {//用户主动切断
}
else
{
//客户端切换
[self duankaitixing];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[selfconnectToHost];
}
}
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"didConnectToHost");
if ([sock isConnected] ==NO) {
[selfduankaitixing];
}
[selfsendshuju];
[receiveSocketreadDataWithTimeout:-1tag:0];
}
//- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket
//{
// NSLog(@"sock:%@:%d",newSocket.connectedHost,newSocket.connectedPort);
// [self.at addObject:newSocket];
//}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
BOOL bo = [receiveSocketisConnected];
if (bo == NO) {
[receiveSocketdisconnect];
}
NSString* strings = [[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"didReadData---%@",strings);
[self didRead:strings];
[receiveSocketreadDataWithTimeout:-1tag:0];
}
#define LTHEAD @"\f\rFD"
#define LTEND @"\f\n\rEND"
-(NSArray*)dataArray:(NSString *)netString
{
//字符串以结尾比较
BOOL isCompleteHead =[netString hasPrefix:LTHEAD];
//字符串以开头比较
BOOL isCompleteEnd =[netString hasSuffix:LTEND];
if (!isCompleteHead) {
//如果不是正常开头,将上次收尾连接入本次串
netString = [NSString stringWithFormat:@"%@%@",someProperty,netString];
// someProperty = @"";
}
NSMutableArray* array = [NSMutableArrayarrayWithArray:[netString componentsSeparatedByString:LTEND]];
if (!isCompleteEnd ) {
//如果结尾不完整,将本次收尾保存
if (array.count>0) {
someProperty = [array lastObject];
[array removeLastObject];
}
}
return array;
}
@end