Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Request for a simple example of passing these parameters GCDWebServerOption_AutomaticallySuspendInBackground and GCDWebServerOption_ConnectedStateCoalescingInterval #333

Closed
yustein opened this issue Aug 22, 2017 · 5 comments
Labels

Comments

@yustein
Copy link

yustein commented Aug 22, 2017

Hi @swisspol ,

I've read all the documentation but couldn't find a code example of how to set the GCDWebServerOption_AutomaticallySuspendInBackground to NO

For example, is there an easy way to include it here: [_webServer startWithPort:8080 bonjourName:nil]; no matter what I tried I've been getting compile errors.

Also it would be nice to learn how to enable this in the GCDWebUploader mode

Of course all above is true for the elusive GCDWebServerOption_ConnectedStateCoalescingInterval setting which is related to running in the background.

I do appreciate for all the time you have spent for this great project, thank you! 👍

@swisspol
Copy link
Owner

Use one of the start methods that take options instead, then just pass the options you want in a dictionary. For instance https://github.com/swisspol/GCDWebServer/blob/master/Mac/main.m#L435.

@yustein
Copy link
Author

yustein commented Aug 22, 2017

Dear @swisspol,

I understand that these concepts are very easy for you. On the other hand anyone with little knowledge can't make heads or tails with it. I don't know why but creating the options like that is giving compile errors.

Isn't it possible inject these changes later?

A simple code sample just puts GCDWebServerOption_ConnectedStateCoalescingInterval to No and runs the server, would be very much appreciated

I've come across the same question on Internet many times. If we can clarify this, it will be helpful for many others.

Also I'm looking for iOS examples.

Thank you for your patience.
Tony

@yustein
Copy link
Author

yustein commented Aug 22, 2017

Hi @swisspol ,

Here is the error I'm getting from your example because the example is for Mac but I'm compiling for iOS, can you help me please?

Why is it that [_webServer startWithPort:8080 bonjourName:nil]; works fine but [_webServer runWithOptions:options error:nil]; returns the error below?


[TRACE] MyWebServerClass.m:45:26: error: use of undeclared identifier 'requestNATPortMapping'
[TRACE]      [options setObject:@(requestNATPortMapping) forKey:GCDWebServerOption_RequestNATPortMapping];
[TRACE]                           ^
[TRACE]  MyWebServerClass.m:46:26: error: use of undeclared identifier 'bindToLocalhost'
[TRACE]      [options setObject:@(bindToLocalhost) forKey:GCDWebServerOption_BindToLocalhost];
[TRACE]                           ^
[TRACE]  MyWebServerClass.m:69:17: error: no visible @interface for 'GCDWebServer' declares the selector 'runWithOptions:error:'
[TRACE]      [_webServer runWithOptions:options error:nil];
[TRACE]       ~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
[TRACE]  3 errors generated.

@yustein
Copy link
Author

yustein commented Aug 22, 2017

Hi @swisspol ,

From the documents, it looks like runWithOptions can't be used on iOS, it is designed for command line use. What do we do to pass the options if we are compiling for iOS?

- (BOOL)runWithOptions:(NSDictionary *)options error:(NSError **)error
Discussion

Runs the server synchronously using -startWithOptions: until a SIGTERM or SIGINT signal is received i.e. Ctrl-C in Terminal. This method is intended to be used by command line tools.

Returns NO if the server failed to start and sets “error” argument if not NULL.

Warning: This method must be used from the main thread only.
Declared In

GCDWebServer.h

@yustein
Copy link
Author

yustein commented Aug 22, 2017

I figured it out, how difficult was it to include this as an example on the readme page I wonder...
It's all in the correct parameters, a simple example like below can make many people's lives much easier.

Btw, it was not runWithOptions it was startWithOptions

    NSMutableDictionary* options = [NSMutableDictionary dictionary];
    [options setObject:@8080 forKey:GCDWebServerOption_Port];
    //BindToLocalhost set to YES means HTTP server won't accept any connections outside of your device
    [options setObject:@YES forKey:GCDWebServerOption_BindToLocalhost];
    [options setObject:@"" forKey:GCDWebServerOption_BonjourName];
    //below parameter NO makes sure the server runs until iOS stops it in the background
    [options setObject:@NO forKey:GCDWebServerOption_AutomaticallySuspendInBackground];
    //the parameter 2.0 below is in seconds. It is defined as double so you need to have x.x type of a number as the parameter
    [options setObject:@2.0 forKey:GCDWebServerOption_ConnectedStateCoalescingInterval];
    
    GCDWebServer* _webServer;
    // Create server
    _webServer = [[GCDWebServer alloc] init];
    
    [_webServer addGETHandlerForBasePath:@"/" directoryPath:NSHomeDirectory() indexFilename:nil cacheAge:3600 allowRangeRequests:YES];
    [_webServer startWithOptions:options error:nil];

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants