如何在长轮询时立即终止连接

问题描述

商店将客户显示器连接到 LAN 上的 192.168.1.68。 它使用长轮询通过 http POST 接收显示数据。

显示发送的数据,但需要 5 次发送才能返回 HttpSendRequest。 我将超时设置为 60 毫秒,重试次数设置为 1,但在 HttpSendRequest 完成之前仍然需要 5 秒。

如果超时值较大,HttpSendRequest 会按预期等待更长的时间。

为什么 HttpSendRequest 的等待时间不能少于 5 秒?

如何在数据发送后立即终止调用,以便单线程应用程序可以继续。命令

curl -d "@duedata.json" -H "Content-Type: application/json; charset=utf-8" --max-time 2 -X POST http://192.168.1.92:8082/poll-command

需要 2 秒并在 PCU 中显示数据。数据发送到显示器需要 1.2 秒。

FUNCTION HTTPGetEx
LParaMETERS tcPage,tcBuffer,tnBufferSize,tcHeaders,tcFileName,lpost


DECLARE INTEGER HttpOpenRequest ;
   IN WININET.DLL ;
   INTEGER hHTTPHandle,;
   STRING lpzReqMethod,;
   STRING lpzPage,;
   STRING lpzVersion,;
   STRING lpzReferer,;
   STRING lpzAcceptTypes,;
   INTEGER dwFlags,;
   INTEGER dwContextw

hHTTPResult=HttpOpenRequest(THIS.hhttpsession,;
   Icase(VARTYPE(lpost)='C',lpost,lpost or  tnPostSize > 0,"POST","GET"),;
   tcPage,;
   NULL,NULL,;
   INTERNET_FLAG_RELOAD + ;
   IIF(THIS.lsecurelink,INTERNET_FLAG_SECURE,0) + ;
   this.nHTTPServiceFlags,0) 

*** Apply timeout to the HTTP connection handle
THIS.wininetsettimeout(THIS.nConnectTimeOut,hHTTPResult)
THIS.wininetsettimeout(THIS.nConnectTimeOut)

THIS.hhttpsession=hHTTPResult

DECLARE INTEGER HttpSendRequest    ;
   IN WININET.DLL ;
   INTEGER hHTTPHandle,;
   STRING lpzHeaders,;
   INTEGER cbHeaders,;
   STRING lpzPost,;
   INTEGER cbPost

dwTimeoutSecs = 60

llRetVal=InternetSetoption(hHTTPResult,;
   INTERNET_OPTION_RECEIVE_TIMEOUT,;
   @dwTimeoutSecs,4)

llRetVal=InternetSetoption(hHTTPResult,;
   INTERNET_OPTION_SEND_TIMEOUT,4)


dwTimeoutSecs=1  &&// Retry only 1 time
llRetVal=InternetSetoption( hHTTPResult,;
      INTERNET_OPTION_CONNECT_RETRIES,;
      @dwTimeoutSecs,4)

? 'Before HttpSendRequest'
* Todo: why HttpSendRequest takes 5 seconds ?
lnRetval=HttpSendRequest(hHTTPResult,LEN(tcHeaders),lcPostBuffer,tnPostSize)
? 'After HttpSendRequest'

解决方法

使用 winhttprequest:

检查https://docs.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-settimeouts

Clear
owinhttp  = Create('winhttp.winhttprequest.5.1')
owinhttph = Create("winhttpH")
Eventhandler(owinhttp,owinhttph)

url = 'https://stackoverflow.com/questions/66857697/how-to-terminate-connection-immediately-on-long-polling'

Try

    With owinhttp
* resolveTimeout,connectTimeout,sendTimeout,receiveTimeout: 
        .setTimeOuts(2000,30000,60000)
        .Open('GET',url,.F.)
        .setrequestheader('Connection','keep-alive')
        .Send()
    Endwith

Catch

    Aerror(ae)
    If ae(1,1) # 1429
        Display Memory Like ae
    Endif

Endtry


*****************************************************
Define Class winhttph As Session OlePublic
*****************************************************

    Implements iwinhttprequestevents In winhttp.winhttprequest.5.1
    bytesrec = 0
    cData = ''

*-------------------------------------------------------------------------------------
Procedure iwinhttprequestevents_onresponsedataavailable(Data As variant) As void
*-------------------------------------------------------------------------------------

    This.bytesrec = This.bytesrec+Len(Data)
    This.cData = This.cData+Data

    ? 'Bytes received:',This.bytesrec
    ? Strconv(Data,11)

    If This.bytesrec > 2000
        owinhttp.abort()
        Strtofile(Ttoc(Datetime())+Chr(13)+This.cData,'test.txt')
        Modify File test.txt Nowait
    Endif

*--------------------------------------------------------------------
Procedure iwinhttprequestevents_onresponsestart(Status As Number,contenttype As String) As void
*--------------------------------------------------------------------

*--------------------------------------------------------------------
Procedure iwinhttprequestevents_onerror(errornumber As Number,errordescription As String) As void
*--------------------------------------------------------------------
if errorNumber # -2147024890
    ? 'ERROR',errornumber,errordescription,'url:'+url
endif

*--------------------------------------------------------------------
Procedure iwinhttprequestevents_onresponsefinished() As void
*--------------------------------------------------------------------

********************************************
Enddefine
********************************************