iOS本地服务器-GCDWebServer支持后台运行

Posted in :

遇到的问题:

GCDWebServer不支持长链接,只要App退到后台,connect自动就stop,网页请求无法获得响应。

解决办法:

1)开启支持后台模式:将GCDWebServer.m中的GCDWebServerOption_AutomaticallySuspendInBackground设置为NO;

修改為:

_suspendInBackground = [_GetOption(_options, GCDWebServerOption_AutomaticallySuspendInBackground, @NO) boolValue];

2)打开Background Modes;

結果還是不行。


修改為 @NO 的 xcode console log:

[ERROR] Failed accepting IPv4 socket: Bad file descriptor (9)
[DEBUG] Did open connection on socket 6
[DEBUG] Connection received 363 bytes on socket 6
[DEBUG] Connection on socket 6 preflighting request "GET /" with 363 bytes body
[DEBUG] Connection on socket 6 processing request "GET /" with 363 bytes body
[DEBUG] Did connect
[DEBUG] Did start background task
[DEBUG] Connection sent 182 bytes on socket 6
[ERROR] Error while writing to socket 6: Broken pipe (32)
[DEBUG] Did close connection on socket 6
[VERBOSE] [192.168.31.237:8080] 192.168.31.237:56991 200 "GET /" (363 | 182)
[DEBUG] Did disconnect
[DEBUG] Did end background task

修改為 @YES 的 console log 一切換到背景,web server網路就斷了:

[DEBUG] Did open IPv4 listening socket 4
[DEBUG] Did open IPv6 listening socket 5
[INFO] GCDWebServer started on port 8080 and reachable at http://192.168.31.237:8080/
2018-03-19 17:09:23.227 TkWebServer[3414:1442070] Visit http://192.168.31.237:8080/ in your web browser
[DEBUG] Did enter background
[DEBUG] Did close IPv6 listening socket 5
[DEBUG] Did close IPv4 listening socket 4
[INFO] GCDWebServer stopped

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *