dhbaird / easywsclient

A short and sweet WebSocket client for C++
MIT License
741 stars 205 forks source link

parser failing on valid stream urls. #75

Open davidawad opened 6 years ago

davidawad commented 6 years ago

I'm trying to subscribe to the following stream url.

$ wsta wss://stream.binance.com:9443/ws/bnbbtc@depth
Connected to wss://stream.binance.com:9443/ws/bnbbtc@depth
{"e":"depthUpdate","E":1522849782404,"s":"BNBBTC","U":56348828,"u":56348828,"b":[["0.00177540","205.23000000",[]]],"a":[]}
{"e":"depthUpdate","E":1522849783404,"s":"BNBBTC","U":56348829,"u":56348829,"b":[["0.00176290","0.00000000",[]]],"a":[]}
# . . . 

I keep hitting this line when trying to run the following code to read a stream from binance.

I think the regex being used there needs to be changed.

here's the cpp i'm running.

#include "easywsclient.hpp"
#include "easywsclient.cpp"

#ifdef _WIN32
#pragma comment( lib, "ws2_32" )
#include <WinSock2.h>
#endif

#include <assert.h>
#include <stdio.h>
#include <string>
#include <memory>

int main()
{
    using easywsclient::WebSocket;

// #ifdef _WIN32
    // INT rc;
    // WSADATA wsaData;

    // rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
    // if (rc) {
        // printf("WSAStartup Failed.\n");
        // return 1;
    // }
// #endif

    std::unique_ptr<WebSocket> ws(WebSocket::from_url("wss://stream.binance.com:9443/ws/bnbbtc@depth"));
    assert(ws);
    // ws->send("goodbye");
    // ws->send("hello");
    //
    while (ws->getReadyState() != WebSocket::CLOSED) {
        WebSocket::pointer wsp = &*ws; // <-- because a unique_ptr cannot be copied into a lambda
        ws->poll();
        ws->dispatch([wsp](const std::string & message) {
            printf(">>> %s\n", message.c_str());
            if (message == "world") { wsp->close(); }
        });
    }

// #ifdef _WIN32
    // WSACleanup();
// #endif

    // N.B. - unique_ptr will free the WebSocket instance upon return:
    return 0;
}

here's my error:

$ g++ -I. -Wall -stdlib=libc++ -std=c++1z read_stream.cpp -o client

$ ./client                            09:50:21
ERROR: Could not parse WebSocket url: wss://stream.binance.com:9443/ws/bnbbtc@depth
Assertion failed: (ws), function main, file read_stream.cpp, line 36.
fish: './client' terminated by signal SIGABRT (Abort)
dhbaird commented 6 years ago

Hey David, this is a known issue- easywsclient does not support SSL connections. There are some forks however, that have added SSL and you may want to use what they did.

davidawad commented 6 years ago

hey @dhbaird can you link me to one? Thanks!