我正在尝试使用 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(或编码成错误的格式)
最佳答案
我想出了我的问题。使用 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}}
}}
关于c++ - 尝试发送第二个有效载荷后“解码有效载荷时出错”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39678143/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我正在使用Postgres.app在OSX(10.8.3)上。我已经修改了我的PATH,以便应用程序的bin文件夹位于所有其他文件夹之前。Rammy:~phrogz$whichpg_config/Applications/Postgres.app/Contents/MacOS/bin/pg_config我已经安装了rvm并且可以毫无错误地安装pggem,但是当我需要它时我得到一个错误:Rammy:~phrogz$gem-v1.8.25Rammy:~phrogz$geminstallpgFetching:pg-0.15.1.gem(100%)Buildingnativeextension