尝试发送第二个有效负载后'Error while decoding payload'



我正在尝试使用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;
}

(显然这段代码是简化的,只用于连接到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(或编码成错误的格式)

我找到我的问题了。使用discordia源代码作为参考,我发现我错误地创建了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}}
}}

相关内容

  • 没有找到相关文章

最新更新