BTCTrader / broker-api-docs

The documentation for BTCTrader's white label exchange platform API. Use this documentation to access the APIs of BTCTurk other BTCTrader partners.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"HS: ACCEPT missing"

emirhanyasin opened this issue · comments

Hi,

Trying to connect to ws-feed-pro.btcturk.com using C++ libwebsockets library. Every time I try to connect, "HS: ACCEPT missing" error is returned. This error generally means that the WebSocket server didn't respond with a valid WebSocket handshake. The server side (ws-feed-pro.btcturk.com) do not return an ACCEPT message in its header to my handshake request. ( I can connect to stream.binance.com with the exact same code. )

  • Is there any special SSL cert we need to use?
  • What can be the cause of this problem?

Here I am sharing the code:

#include <libwebsockets.h>
#include <string>
#include <iostream>

static int callback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len)
{
    switch (reason) {
    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
        std::cout << "Client connection error" << (char *)in << std::endl;
        break;
    case LWS_CALLBACK_CLIENT_ESTABLISHED:
        std::cout << "Client has connected to server" << std::endl;
        break;
    case LWS_CALLBACK_CLIENT_RECEIVE:
        std::cout << "Received data: " << (char *)in << std::endl;
        break;
    default:
        break;
    }
    return 0;
}

struct lws_protocols protocols[] =
{
	{
		"btcturk-protocol",
		callback,
		0,
		8092,
	},
	{ NULL, NULL, 0, 0 } 
};

int main(int argc, char *argv[])
{
    struct lws_context_creation_info info;
    struct lws_client_connect_info ccinfo = {0};
    struct lws_context *context;
    struct lws *wsi;
    const char *message = "{\"type\":\"subscribe\",\"channels\":[{\"name\":\"ticker\",\"markets\":[\"BTCTRY\"]}]}";
    memset(&info, 0, sizeof info);

    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
    info.gid = -1;
    info.uid = -1;
    info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

    context = lws_create_context(&info);
    if (context == nullptr) {
        std::cout << "Creating libwebsocket context failed" << std::endl;
        return -1;
    }

    ccinfo.context = context;
    ccinfo.address = "ws-feed-pro.btcturk.com";
    ccinfo.port = 443;
    ccinfo.path = "/";
    ccinfo.host = lws_canonical_hostname(context);
    ccinfo.origin = "origin";
    ccinfo.protocol = protocols[0].name;
    ccinfo.ssl_connection = LCCSCF_USE_SSL;

    wsi = lws_client_connect_via_info(&ccinfo);
    if (wsi == nullptr) {
        std::cout << "libwebsocket connect failed" << std::endl;
        return -1;
    }

    lws_callback_on_writable(wsi);
    unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + strlen(message) + LWS_SEND_BUFFER_POST_PADDING];
    unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
    size_t n = strlen(message) + 1;
    strncpy((char *)p, message, n);
    lws_write(wsi, p, n, LWS_WRITE_TEXT);

    while (true) {
        lws_service(context,  500);
    }

    lws_context_destroy(context);

    return 0;
}