Last active
August 29, 2015 14:12
-
-
Save KJTsanaktsidis/0847a323ee321712a966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef gcd_test_test_webserver_h | |
#define gcd_test_test_webserver_h | |
@interface TestWebserver : NSObject | |
-(TestWebserver*)init; | |
-(void) start; | |
-(void) stop; | |
@end | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "TestWebserver.h" | |
#import "GCDWebServer.h" | |
#import "GCDWebServerDataRequest.h" | |
#import "GCDWebServerDataResponse.h" | |
#import "GCDWebServerStreamedResponse.h" | |
@implementation TestWebserver | |
{ | |
GCDWebServer* _server; | |
} | |
-(TestWebserver*) init; | |
{ | |
self->_server = [[GCDWebServer alloc] init]; | |
dispatch_time_t half_second = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)); | |
[self->_server | |
addDefaultHandlerForMethod:@"GET" | |
requestClass:[GCDWebServerDataRequest class] | |
asyncProcessBlock:^(GCDWebServerRequest *request, GCDWebServerCompletionBlock completionBlock) { | |
//Wait half a second before sending headers | |
dispatch_after(half_second, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
GCDWebServerStreamedResponse* res = | |
[[GCDWebServerStreamedResponse alloc] | |
initWithContentType:@"text/plain" | |
asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock dataCompletionBlock) { | |
NSLog(@"Sending abcdefg"); | |
dataCompletionBlock([@"abcdefg" dataUsingEncoding:NSUTF8StringEncoding], nil); | |
dispatch_after(half_second, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
NSLog(@"Sending hijklmnop"); | |
dataCompletionBlock([@"hijklmnop" dataUsingEncoding:NSUTF8StringEncoding], nil); | |
dataCompletionBlock([[NSData alloc] init], nil); | |
NSLog(@"ended"); | |
}); | |
}]; | |
completionBlock(res); | |
}); | |
}]; | |
return self; | |
} | |
-(void) start | |
{ | |
[self->_server startWithPort:10001 bonjourName:nil]; | |
} | |
-(void) stop | |
{ | |
[self->_server stop]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment