3

I want just to add WebSockets to my app that uses WinHTTP in async mode.

When I need a WebSocket I call the following.

  1. Before sending request:

    WinHttpSetOption(context->hRequest, WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET, NULL, 0);
    
  2. In WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:

    appContext->pIoRequest->hWebSocketHandle = WinHttpWebSocketCompleteUpgrade(appContext->hRequest, NULL);
    WinHttpWebSocketReceive(appContext->pIoRequest->hWebSocketHandle, appContext->pszOutBuffer,RESPONSE_BUFFER_SIZE, NULL, NULL);
    

all without errors.

Now I see in Fiddler that the server sends some data to my WebSocket but there is no WINHTTP_CALLBACK_STATUS_READ_COMPLETE triggered.

Any ideas why this is? How can I read asynchronously from my WebSocket? Sending data to the WebSocket works well.

Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
Bigconnect
  • 61
  • 4

2 Answers2

3

Omg! I found how its work!

  1. You MUST call additional WinHttpSetStatusCallback to set WebSocket callback for WebSocketHandle returned in WinHttpWebSocketCompleteUpgrade and this callback MUST differ then that from call WinHttpWebSocketCompleteUpgrade was made!
  2. It is no possible to set a context pointer by WinHttpSetOption with WINHTTP_OPTION_CONTEXT_VALUE flag! Its not work. dwContext In WebSocketCallback has wrong data. Call to WinHttpQueryOption in WebSocketCallback return wrong context data. I think that is a BUG in Windows 8.1. I write my own handler to connect my context with WebSocketHandle.

All of this is NOT documented in MSDN! Most of all, I did not google any info about async winhttp websocket usage... So, I am the first=) I will be very glad if my research will help you!

Bigconnect
  • 61
  • 4
  • Do you happen have a sample of the working code? I'm running into similar issues and can't find references of async WinHTTP WebSockets. – juniel_katarn Mar 13 '20 at 07:38
0

It seems websockets do not get PING and PONG messages to the callback!

Bigconnect
  • 61
  • 4