【问题标题】:GCDWebServer run -addHandlerForMethod: twice time with different path, second request failed?GCDWebServer 运行 -addHandlerForMethod:两次不同路径,第二次请求失败?
【发布时间】:2015-09-10 08:31:04
【问题描述】:

我使用GCDWebServer,使用不同的参数“path”运行方法-addHandlerForMethod:path:requestClass:两次。然后 NSLog webServer.serverURL,

  • 第一次成功:192.168.0.121:8080,
  • 但第二次失败:nil

为什么?请帮帮我。

#import "ServerMock.h"

@implementation ServerMock

+ (void)mockWithMethod:(NSString *)method path:(NSString *)path timeoutInterval:(NSTimeInterval)timeoutInterval JSONObject:(NSDictionary *)JSONObject port:(NSUInteger)port serverURL:(void (^)(NSURL *serverURL))block
{
GCDWebServer *webServer = [GCDWebServer new];

[webServer addHandlerForMethod:method path:path requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(GCDWebServerRequest *request, GCDWebServerCompletionBlock completionBlock) {

    GCD_DELAY_AFTER(timeoutInterval, ^{
        GCDWebServerDataResponse *response = [GCDWebServerDataResponse responseWithJSONObject:JSONObject];
        completionBlock(response);
    });
}];

[webServer startWithPort:port bonjourName:nil];

block(webServer.serverURL);
}

@end

//////////////////////////////////////

- (void)viewDidLoad
{
NSDictionary *dict = @{

                       @"11111": 
                       @"22222222"

                       };

[ServerMock mockWithMethod:@"GET" 
                      path:@"/123" 
           timeoutInterval:0 
                JSONObject:dict 
       port:8080 serverURL:^(NSURL *serverURL) {

    NSLog(@"________%@", serverURL);
}];


NSDictionary *dict2 = @{ @"2222222": @"111111111"};

[ServerMock mockWithMethod:@"GET" path:@"/321" timeoutInterval:0 JSONObject:dict2 port:8080 serverURL:^(NSURL *serverURL) {

    NSLog(@"________%@", serverURL);
}];
}

【问题讨论】:

    标签: objective-c gcdwebserver


    【解决方案1】:

    检查 Xcode 控制台是否有错误。问题很可能是您在调用块后没有停止 GCDWebServer 实例,因此它仍在运行并保持在端口 8080 上,从而阻止新服务器启动。

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2017-05-20
      • 1970-01-01
      • 2020-03-28
      • 2011-05-02
      • 2011-09-02
      • 1970-01-01
      • 2019-08-03
      • 2023-01-25
      相关资源
      最近更新 更多