Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Gnomorian/ac2c3a73ec52278e8477efdb42bf39aa to your computer and use it in GitHub Desktop.
Save Gnomorian/ac2c3a73ec52278e8477efdb42bf39aa to your computer and use it in GitHub Desktop.
websocket.h, WebSocketEndClientHandshake not working for some unspecified reason
#include <Windows.h>
#include <websocket.h>
#include <iostream>
#include <winerror.h>
ULONG RecieveBuffer = 4096;
ULONG SendBuffer = 4096;
BOOL DisableMasking = false; // by default, this is not used. try disabling it first
BOOL DisableUTF8Verification = false;
int main() {
WEB_SOCKET_HANDLE wshandle;
// pProperties - pointer to an array of properties for the sockets
// ulPropertyCount - how many properties are in the array
// *phWebSocket - a handle that will be written to if successful
if (WebSocketCreateClientHandle(NULL, 0, &wshandle) != S_OK) {
std::cout << "WebSocketCreateClientHandle Failed" << std::endl;
return 1;
}
// a header structure, "Host" is the bare-minimum.
WEB_SOCKET_HTTP_HEADER host;
PCHAR phost = "Host";
PCHAR pValue = "wss://cpclientapi.softphone.com:9002/counterpath/socketapi/v1";
host.pcName = phost;
host.ulNameLength = sizeof(phost);
host.pcValue = pValue;
host.ulValueLength = sizeof(pValue);
WEB_SOCKET_HTTP_HEADER headers[1] = {host};
WEB_SOCKET_HTTP_HEADER* responceHeaders = nullptr;
ULONG responceHeadderCount;
// web socket handle
// pointer to an array of sub-protocols
// how many sub-protocols in the array
// number of extensions in pszEntensions
// pointer to an array of request WEB_SOCKET_HTTP_HEADER structures
// number of request headders
// on succcess pointer to an array of responce WEB_SOCKET_HTTP_HEADER structures
// on success number of responce headders
if (WebSocketBeginClientHandshake(wshandle, NULL, 0, NULL, 0, headers, 1, &responceHeaders, &responceHeadderCount) != S_OK) {
return 1;
}
// write the responce hadders
// does this indecate what pulSelectedExtensions are required?
for (int i = 0; i < responceHeadderCount; i++) {
std::cout << responceHeaders[i].pcName << std::endl;
}
ULONG* pulSelectedSubprotocol = nullptr;
ULONG* pulSelectedSubprotocolCount = nullptr;
ULONG* pulSelectedExtensions = nullptr;
ULONG* pulSelectedExtensionCount = nullptr;
HRESULT result = WebSocketEndClientHandshake(wshandle, responceHeaders, responceHeadderCount, pulSelectedExtensions, pulSelectedExtensionCount, NULL);
if (result != S_OK) {
switch (result) {
case E_INVALID_PROTOCOL_FORMAT:
std::cout << "Protocol data had an invalid format." << std::endl;
break;
// case E_UNSUPPORTED_SUBPROTOCOL:
// std::cout << "Protocol data had an invalid format." << std::endl;
// break;
// case E_UNSUPPORTED_EXTENSION:
// std::cout << "Protocol data had an invalid format." << std::endl;
// break;
default:
std::cout << result << std::endl;
}
char stop;
std::cin >> stop;
return result;
}
std::cout << pulSelectedSubprotocol[0] << std::endl;
char end;
std::cin >> end;
// cleanup
WebSocketDeleteHandle(wshandle);
return 0;
//WebSocketBeginClientHandshake();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment