尝试发送第二个有效载荷后,"解码有效载荷时出错"

Orf*_*fby 9 c++ boost-asio websocket

我正在尝试使用websocketpp库实现连接到WebSocket(精确的Discord网关)的客户端,但是当我尝试将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;
}
Run Code Online (Sandbox Code Playgroud)

(显然,此代码已简化,仅用于连接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.]
Run Code Online (Sandbox Code Playgroud)

编辑:

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

Orf*_*fby 5

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

{"token", token},
{"properties", {
    {"$os", "linux"},
    {"$browser", "my_library"},
    {"$device", "my_library"},
    {"$referrer", ""},
    {"$referring_domain", ""}
}},
{"compress", false},
{"large_threshold", 250},
{"shard", {0, 1}}
Run Code Online (Sandbox Code Playgroud)

到:

{"op", 1},
{"d", {
    {"token", token},
    {"properties", {
        {"$os", "linux"},
        {"$browser", "orfbotpp"},
        {"$device", "orfbotpp"},
        {"$referrer", ""},
        {"$referring_domain", ""}
    }},
    {"compress", false},
    {"large_threshold", 250},
    {"shard", {0, 1}}
}}
Run Code Online (Sandbox Code Playgroud)