1. 程式人生 > >iOS本地伺服器-GCDWebServer支援後臺執行

iOS本地伺服器-GCDWebServer支援後臺執行

遇到的問題:

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