ios 队列

//

//  HallData.m

//  TrueWord

//

//  Created by king454 on 13-12-11.

//  Copyright (c) 2013年 king454. All rights reserved.

//


#import "HallData.h"

#import "DefinClass.h"

#import "ChatCell.h"

#import "HallViewController.h"


static inline NSString *cachesDir(){

    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

}

@implementation HallData


-(id)init

{

    self=[super init];

    if (self) {

        self.user=[[UserModel alloc]init];

        if (![self queue]) {

            self.queue=[[[ASINetworkQueue alloc]init]autorelease];

            [self queue].maxConcurrentOperationCount = 4;

            [ _queue setShouldCancelAllRequestsOnFailure:NO ];

        }

    }

    return self;

}


#pragma mark 加入房间

-(void)joinRoom:(NSString*)userjid andRoomJid:(NSString*)roomjid

{

    //先拼接JSON发送给PHP

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:JOINROOMPHP]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"join" forKey:@"message"];

    [request setPostValue:userjid forKey:@"userjid"];

    [request setPostValue:roomjid forKey:@"roomjid"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

    [_delegate showActivity];

    

}

#pragma mark 退出房间

-(void)exitRoom:(NSString*)userjid andRoomJid:(NSString*)roomjid

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:EXITROOMPHP]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"exit" forKey:@"message"];

    [request setPostValue:userjid forKey:@"userjid"];

    [request setPostValue:roomjid forKey:@"roomjid"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

    

}

#pragma mark 存入图片

-(void)postImage:(NSString*)fileName

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPAUDIOORIMAGE]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"image" forKey:@"message"];

    NSString* filePath=[cachesDir() stringByAppendingPathComponent:fileName];

    NSData* fileData=[NSData dataWithContentsOfFile:filePath];

    [request setData:fileData withFileName:fileName andContentType:nil forKey:@"image"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

}

#pragma mark 存入声音

-(void)postAudio:(NSString*)fileName

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPAUDIOORIMAGE]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"audio" forKey:@"message"];

    NSString* filePath=[cachesDir() stringByAppendingPathComponent:fileName];

    NSData* fileData=[NSData dataWithContentsOfFile:filePath];

    [request setData:fileData withFileName:fileName andContentType:nil forKey:@"audio"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

    

}

#pragma mark 通过传入token返回一个值

-(void)getUser:(NSString*)token

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:SELECTUSERPHP]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"token" forKey:@"message"];

    [request setPostValue:token forKey:@"token"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

    

    

}

#pragma mark 根据roomjid查找

-(void)getUsers:(NSString*)roomjid

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:SELECTUSERWITHROOM]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"roomjid" forKey:@"message"];

    [request setPostValue:roomjid forKey:@"roomjid"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

}

-(void)getRoomInformationByPHP

{

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:GETROOMINFORMATION]];

    request.userInfo=[NSDictionary dictionaryWithObject:@"room" forKey:@"message"];

    [request setDelegate:self];

    [self.queue addOperation:request];

    [_queue go];

}



#pragma mark 异步代理

- (void)requestFinished:(ASIHTTPRequest *)request

{

    if ([_delegate  isMemberOfClass:[HallViewController class]]) {

        [_delegate removeActivity];

    }

    

    if (!self) {

        return;

    }

    NSData *responseData = [request responseData];

    NSArray* arr=[NSJSONSerialization JSONObjectWithData:responseData options:1 error:nil];

    NSMutableDictionary *dic = arr[0];

    if (_delegate) {

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"image"]) {

            if ([_delegate respondsToSelector:@selector(requestFinishedByImage:)]) {

                [_delegate requestFinishedByImage:dic];

            }

        }

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"audio"]) {

            if ([_delegate respondsToSelector:@selector(requestFinishedByAudio:)]) {

                [_delegate requestFinishedByAudio:dic];

            }

        }

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"token"]) {

            if ([_delegate respondsToSelector:@selector(requestFinishedByToken:)]) {

                [_delegate requestFinishedByToken:dic];

            }

        }

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"roomjid"]) {

            if ([_delegate respondsToSelector:@selector(requestFinishedByRoomjid:)]) {

                [_delegate requestFinishedByRoomjid:dic];

            }

        }

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"room"]) {

            if ([_delegate respondsToSelector:@selector(requestFinishedByRoomInformation:)]) {

                [_delegate requestFinishedByRoomInformation:dic];

            }

        }

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]) {

            

        }

    }

    //取出返回结果,是否为1

    //    NSLog(@"string==%@",[dic objectForKey:@"result"]);

    

}


- (void)requestFailed:(ASIHTTPRequest *)request

{

    if ([_delegate  isMemberOfClass:[HallViewController class]]) {

        [_delegate removeActivity];

    }

    NSError *error = [request error];

    if (error) {

        if ([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]){

            [self makeAlert:@"加入房间失败"];

        }

        if([[request.userInfo objectForKey:@"message"] isEqualToString:@"exit"]){

            [self makeAlert:@"退出房间失败"];

        }

        if([[request.userInfo objectForKey:@"message"] isEqualToString:@"join"]){

            [self makeAlert:@"加入房间失败"];

        }

    }

}

-(void)makeAlert:(NSString*)name

{

    UIAlertView *alert=[[[UIAlertView alloc]initWithTitle:name

                                                  message:@"一只蜗牛向你奔来"

                                                 delegate:self

                                        cancelButtonTitle:@"确定"

                                        otherButtonTitles:nil, nil]autorelease];

    [alert show];

}


- (void)dealloc

{

    [_queue cancelAllOperations];

    [_queue release];

    [_user release];

    [super dealloc];

    self = nil;

}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值