Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Error while decoding payload' after trying to send second payload

I'm trying to implement a client which connects to a WebSocket (the Discord gateway to be precise) using the websocketpp library, but I'm getting an error when I try to send a JSON payload to the server

The code I'm using is:

//Standard C++:
#include <string>
//JSON Header (nlohmann's library):
#include <json.hpp>
//Networking Headers:
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>
#define WEBSOCKETPP_STRICT_MASKING

std::string token;

static websocketpp::lib::shared_ptr<boost::asio::ssl::context> on_tls_init(websocketpp::connection_hdl)
{
    websocketpp::lib::shared_ptr<boost::asio::ssl::context> ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);

    ctx->set_options(boost::asio::ssl::context::default_workarounds |
                     boost::asio::ssl::context::no_sslv2 |
                     boost::asio::ssl::context::no_sslv3 |
                     boost::asio::ssl::context::single_dh_use);

    return ctx;
}

void onMessage(websocketpp::client<websocketpp::config::asio_tls_client>* client, websocketpp::connection_hdl hdl, websocketpp::config::asio_tls_client::message_type::ptr msg)
{
    //Get the payload
    nlohmann::json payload = nlohmann::json::parse(msg->get_payload());
    //If the op code is 'hello'
    if (payload.at("op") == 10)
    {
        //HEARTBEAT STUFF HAS BEEN REMOVED FOR SIMPLICITY
        //Create the identity JSON
        nlohmann::json identity =
        {
            {"token", token},
            {"properties", {
                {"$os", "linux"},
                {"$browser", "my_library"},
                {"$device", "my_library"},
                {"$referrer", ""},
                {"$referring_domain", ""}
            }},
            {"compress", false},
            {"large_threshold", 250},
            {"shard", {0, 1}}
        };
        //Create the error code object
        websocketpp::lib::error_code errorCode;
        //Send the identity JSON
        client->send(hdl, std::string(identity.dump()), websocketpp::frame::opcode::text, errorCode);
        //If the request was invalid
        if (errorCode) {std::cerr << "Identify handshake failed because " << errorCode.message() << std::endl;}
    }
}

int main(int argc, char** argv)
{
    if (argc > 1)
    {
        //Set the token
        token = argv[1];
    }
    else
    {
        std::cout << "Error, please specify the token as an argument to this program" << std::endl;
        return -1;
    }

    //Create the client
    websocketpp::client<websocketpp::config::asio_tls_client> client;
    client.set_tls_init_handler(on_tls_init);
    client.init_asio();

    client.set_access_channels(websocketpp::log::alevel::all);

    client.set_message_handler(websocketpp::lib::bind(&onMessage, &client, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2));

    //Create an error object
    websocketpp::lib::error_code errorCode;
    //Get the connection from the gateway (usually you'd use GET for the URI, but I'm hardcoding it for simplicity)
    websocketpp::client<websocketpp::config::asio_tls_client>::connection_ptr connection = client.get_connection("wss://gateway.discord.gg/?v=5&encoding=json", errorCode);
    //Check for errors
    if (errorCode)
    {
        std::cout << "Could not create an connection because " << errorCode.message() << std::endl;
    }

    //Connect
    client.connect(connection);

    //Run it
    client.run();

    return 0;
}

(Obviously this code is simplified and is only for connecting to the Discord gateway and sending the payload)

When I do this, I get this output in my terminal:

[2016-09-24 16:36:47] [connect] Successful connection
[2016-09-24 16:36:48] [connect] WebSocket Connection 104.16.60.37:443 v-2 "WebSocket++/0.7.0" /?v=5&encoding=json 101
[2016-09-24 16:36:48] [frame_header] Dispatching write containing 1 message(s) containing 8 header bytes and 238 payload bytes
[2016-09-24 16:36:48] [frame_header] Header Bytes: 
[0] (8) 81 FE 00 EE C3 58 3C 0C 

[2016-09-24 16:36:48] [frame_payload] Payload Bytes: 
[0] (238) [1] �z_c�(Ni�+6�9P�t`�*[i�,T~�+Tc�<6�m
                                                     �(Nc�=Nx�=O.�#(�*S{�=N.�zQu�4Un�9Nu�t(�=Je�=6�5ES�1^~�*E.�zc�z.�1Ry�z.�*Yj�*Ni�z.�t(�=Zi�*Ub�Xc�9Ub�b.�t�9Nh�bg<�ia �,Sg�66�VI�xg�Fg�hU�VI�VU�v�+w{�hW;�KM�<RA� ch�f8�
                                            Mv�k8�%

[2016-09-24 16:36:49] [control] Control frame received with opcode 8
[2016-09-24 16:36:49] [frame_header] Dispatching write containing 1 message(s) containing 6 header bytes and 31 payload bytes
[2016-09-24 16:36:49] [frame_header] Header Bytes: 
[0] (6) 88 9F 07 DD CB 5A 

[2016-09-24 16:36:49] [frame_payload] Payload Bytes: 
[0] (31) [8] 08 7F 8E 28 75 B2 B9 7A 70 B5 A2 36 62 FD AF 3F 64 B2 AF 33 69 BA EB 2A 66 A4 A7 35 66 B9 E5 

[2016-09-24 16:36:49] [error] handle_read_frame error: websocketpp.transport:8 (TLS Short Read)
[2016-09-24 16:36:49] [disconnect] Disconnect close local:[1006,TLS Short Read] remote:[4002,Error while decoding payload.]

EDIT:

After doing some research, it appears the error is caused by the gateway rejecting the request, so I assume websocketpp isn't correctly encoding the JSON (or encoding into the wrong format)

like image 704
Orfby Avatar asked Sep 24 '16 15:09

Orfby


1 Answers

I figured out my problem. Using the discordia source code as a reference I discovered I was creating the JSON incorrectly, so I changed this code:

{"token", token},
{"properties", {
    {"$os", "linux"},
    {"$browser", "my_library"},
    {"$device", "my_library"},
    {"$referrer", ""},
    {"$referring_domain", ""}
}},
{"compress", false},
{"large_threshold", 250},
{"shard", {0, 1}}

to:

{"op", 1},
{"d", {
    {"token", token},
    {"properties", {
        {"$os", "linux"},
        {"$browser", "orfbotpp"},
        {"$device", "orfbotpp"},
        {"$referrer", ""},
        {"$referring_domain", ""}
    }},
    {"compress", false},
    {"large_threshold", 250},
    {"shard", {0, 1}}
}}
like image 188
Orfby Avatar answered Oct 15 '22 00:10

Orfby