尝试发送第二个有效负载后,“解码有效负载时出错”

时间:2016-09-24 15:44:26

标签: c++ websocket boost-asio

我正在尝试使用Discord实现连接到WebSocket(websocketpp library网关)的客户端,但是当我尝试发送JSON时出现错误有效负载到服务器

我正在使用的代码是:

//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;
}

(显然此代码已经过简化,仅用于连接Discord网关并发送有效负载)

当我这样做时,我在终端中输出了这个输出:

[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.]

编辑:

在做了一些研究之后,看起来错误是由网关拒绝请求引起的,所以我假设websocketpp没有正确编码JSON(或编码为错误的格式)

1 个答案:

答案 0 :(得分:4)

我发现了我的问题。使用discordia source code作为参考我发现我正在错误地创建JSON,因此我更改了此代码:

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

为:

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