Я кодирую веб-клиент для Steve Web Server.Я сосредоточен на клиент-серверной части, запросах и ответах.WebSocketBeginClientHandshake
не отправляет запрос GET и IP-адрес хоста.
WebSocketEndClientHandshake
вызов функции не удался этой ошибке
0x83760002 (E_INVALID_PROTOCOL_FORMAT).хотя я установил формат протокола записи "ocpp1.6"
Я не знаю, почему он генерирует эту ошибку, пожалуйста, помогите мне с этим
#include <Windows.h>
#include <WebSocket.h>
#include <Assert.h>
#include <StdIo.h>
#include "Transport.h"
void DumpHeaders(
_In_reads_(headerCount) WEB_SOCKET_HTTP_HEADER* headers,
_In_ ULONG headerCount)
{
for (ULONG i = 0; i < headerCount; i++)
{
wprintf(L"%.*S: %.*S\n", headers[i].ulNameLength, headers[i].pcName, headers[i].ulValueLength, headers[i].pcValue);
}
}
HRESULT Initialize(
_Out_ WEB_SOCKET_HANDLE* outClientHandle)
{
HRESULT hr = S_OK;
WEB_SOCKET_HANDLE clientHandle = NULL;
hr = WebSocketCreateClientHandle(NULL, 0, &clientHandle);
if (FAILED(hr))
{
goto quit;
}
printf("hr=%s\n", hr);
printf("clienthandle=%d %s\n", clientHandle,clientHandle);
*outClientHandle = clientHandle;
clientHandle = NULL;
quit:
if (clientHandle != NULL)
{
WebSocketDeleteHandle(clientHandle);
clientHandle = NULL;
}
return hr;
}
HRESULT PerformHandshake(
_In_ WEB_SOCKET_HANDLE clientHandle)
{
HRESULT hr = S_OK;
WEB_SOCKET_HTTP_HEADER* clientAdditionalHeaders = NULL;
WEB_SOCKET_HTTP_HEADER* clientHeaders = NULL;
ULONG clientAdditionalHeaderCount = 0;
ULONG clientHeaderCount = 0;
const WEB_SOCKET_HTTP_HEADER* serverheaders = NULL;
ULONG serverheadercount = 0;
ULONG hostcount = 2;
PCSTR pszSubprotocols = "ocpp1.6";
ULONG pc1 = 1;
WEB_SOCKET_HTTP_HEADER h1 =
{
"HOST",
ARRAYSIZE("HOST") - 1,
"ws://43.225.54.75:19111",
ARRAYSIZE("ws://43.225.54.75:19111")-1
};
WEB_SOCKET_HTTP_HEADER h2=
{
"GET",
ARRAYSIZE("GET") - 1,
"GET /steve/websocket/CentralSystemService/BG007 HTTP/1.1",
ARRAYSIZE("GET /steve/websocket/CentralSystemService/BG007 HTTP/1.1")-1
};
WEB_SOCKET_HTTP_HEADER host[2] = {h2,h1};
ULONG* pulSelectedSubprotocol = nullptr;
ULONG* pulSelectedSubprotocolCount = nullptr;
ULONG* pulSelectedExtensions = nullptr;
ULONG* pulSelectedExtensionCount = nullptr;
hr = WebSocketBeginClientHandshake(
clientHandle,
&pszSubprotocols,
pc1,
NULL,
0,
host,//(_WEB_SOCKET_HTTP_HEADER*)host,
hostcount,
&clientAdditionalHeaders,
&clientAdditionalHeaderCount);
if (FAILED(hr))
{
goto quit;
}
// Concatenate list of headers that the HTTP stack must send (the Host header) with
// a list returned by WebSocketBeginClientHandshake.
clientHeaderCount = clientAdditionalHeaderCount +2;
clientHeaders = new WEB_SOCKET_HTTP_HEADER[clientHeaderCount];
if (clientHeaders == NULL)
{
hr = E_OUTOFMEMORY;
goto quit;
}
CopyMemory(clientHeaders, clientAdditionalHeaders, (clientAdditionalHeaderCount+2) * sizeof(WEB_SOCKET_HTTP_HEADER));
printf("Final client header ....\r\n");
clientHeaders[clientAdditionalHeaderCount+0] = h2;
clientHeaders[clientAdditionalHeaderCount+1] = h1;
DumpHeaders(clientHeaders, clientHeaderCount);
hr = WebSocketEndClientHandshake(
clientHandle,
clientHeaders,//(_WEB_SOCKET_HTTP_HEADER*)serverheaders,
clientHeaderCount,//serverheadercount,
pulSelectedExtensions,
pulSelectedExtensionCount,
pulSelectedSubprotocol
);
printf(" after end client handshake\n");
printf("clientcount=%d\n", clientHeaderCount);
DumpHeaders(clientHeaders, clientHeaderCount);
printf("servercount=%d\n", serverheadercount);
if (hr != S_OK) {
switch (hr) {
case E_INVALID_PROTOCOL_FORMAT:
printf("1...Protocol data had an invalid format\n");
break;
default:
printf("hr=%d\n", hr);
}
}
else
printf("okk\n");
quit:
return hr;
}
int __cdecl wmain()
{
HRESULT hr = S_OK;
WEB_SOCKET_HANDLE clientHandle = NULL;
Transport transport;
hr = Initialize(&clientHandle);
if (FAILED(hr))
{
goto quit;
}
hr = PerformHandshake(clientHandle);
if (FAILED(hr))
{
goto quit;
}
quit:
if (clientHandle != NULL)
{
WebSocketDeleteHandle(clientHandle);
clientHandle = NULL;
}
if (FAILED(hr))
{
wprintf(L"Websocket failed with error 0x%x\n", hr);
return 0;
}
return 1;
}
hr = (null) clienthandle = 10181480 WEBS
Final client header ....
Sec-WebSocket-Key: BtZ3/RzydTPoY3fbOK6kRA==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: ocpp1.6
GET: GET /steve/websocket/CentralSystemService/BG007 HTTP/1.1
HOST: ws://43.225.54.75:19111
after end client handshake
clientcount=7
Sec-WebSocket-Key: BtZ3/RzydTPoY3fbOK6kRA==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: ocpp1.6
GET: GET /steve/websocket/CentralSystemService/BG007 HTTP/1.1
HOST: ws://43.225.54.75:19111
servercount=0
1...Protocol data had an invalid format
Websocket failed with error 0x83760002