描述:
-(void)startConnectSocket{
self.socket =[[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];
if (![self.socket isConnected]) {
NSError * error=nil;
if ( [self.socket connectToHost:HOST onPort:POST error:&error]) {
NSLog(@"%@",[error description]);
}
else{
NSLog(@"失败");
}
}
}
#pragma mark当链接成功时就会调用
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
NSLog(@"%@",host);
// self.HearTimer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(checkLongConnectByServe) userInfo:nil repeats:YES];
// [self checkLongConnectByServe];
// [sock readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:200];
// [self.HearTimer fire];
//等待服务器消息,
NSLog(@"%@",[NSThread currentThread]);
[sock readDataWithTimeout:-1 tag:200];
}
#pragma mark当服务器给客服端发送消息时调用
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSMutableDictionary * dicData= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dicData);
[sock readDataWithTimeout:-1 tag:200];
}
[size=16px] 第一次链接,服务器就会给客服端发送数据,可是接受不到,为什么??[/size]