问题描述
我无法通过名为 websocketpp 的WebSockets C ++库从MtGox API获取信息:
I can't get info from MtGox API via WebSockets C++ library named websocketpp:
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_open(websocketpp::connection_hdl hdl)
{
std::cout << "on_open \n";
}
void on_close(websocketpp::connection_hdl hdl)
{
std::cout << "on_close \n";
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::cout << msg->get_payload() << '\n';
}
int main()
{
client c;
try
{
c.init_asio();
c.set_open_handler(on_open);
c.set_close_handler(on_close);
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection("ws://websocket.mtgox.com:80/mtgox?Currency=EUR", ec);
c.connect(con);
c.run();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
catch (websocketpp::lib::error_code e)
{
std::cout << e.message() << std::endl;
}
catch (...)
{
std::cout << "other exception" << std::endl;
}
}
输出
[2013-11-18 23:10:14] [错误]服务器握手响应无效:无效的HTTP状态.
[2013-11-18 23:10:14] [error] Server handshake response was invalid: Invalid HTTP status.
[2013-11-18 23:10:14] [disconnect]失败:无效的HTTP状态.
[2013-11-18 23:10:14] [disconnect] Failed: Invalid HTTP status.
在调试器中,我看到"403 forbidden".错误,但是我可以通过 http://www.websocket.org/echo.html .
In the debugger i see "403 forbidden" error, but I can use it via services like http://www.websocket.org/echo.html.
我已经尝试使用"ws://socketio.mtgox.com:80/mtgox?Currency = EUR",但收到以下错误:
I already tried to use "ws://socketio.mtgox.com:80/mtgox?Currency=EUR", but received the following error:
[2013-11-18 23:18:08] handle_read_http_response中的[错误]错误:文件结尾
[2013-11-18 23:18:08] [error] error in handle_read_http_response: End of File
[2013-11-18 23:18:08] [断开连接]失败:文件结束
[2013-11-18 23:18:08] [disconnect] Failed: End of File
此代码有什么问题?
推荐答案
MtGox似乎正在执行源过滤.基于浏览器的WebSocket连接将自动发送一个源标头,该源标头与运行脚本的域的值一起发送.由于这主要是对运行可能未知的Javascript代码的浏览器的一种安全措施,因此WebSocket ++默认情况下不会发送原始标头.
MtGox appears to be doing origin filtering. Browser based WebSocket connections will have an origin header sent automatically with the value of the domain that the script is being run from. As this is primarily a security measure for browsers running potentially unknown Javascript code, WebSocket++ does not send an origin header by default.
MtGox似乎可以与我尝试过的任何来源一起使用,只要设置了所有来源即可.大概他们使用它来将他们认为是恶意的来源列入黑名单.您可以使用带有以下代码的WebSocket ++发送一个原始标头(在适合您的应用程序的任何原始位置填充):
MtGox appears to work fine with any origin that I have tried as long as one is set at all. Presumably they use this to blacklist origins that they deem malicious. You can send an origin header using WebSocket++ with the following code (Fill in whatever origin seems appropriate for your application):
con->replace_header("Origin","http://www.example.com");
在请求与endpoint::get_connection
的新连接之后但在调用endpoint::connect
之前运行此命令.
Run this after you request your new connection with endpoint::get_connection
, but before calling endpoint::connect
.
请参见 http://en.wikipedia.org/wiki/Same-origin_policy 有关此处使用的相同来源策略"安全方法的更多详细信息.
See http://en.wikipedia.org/wiki/Same-origin_policy for more details about the 'same-origin policy' security method that is being used here.
这篇关于MtGox API和websocketpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!