我有一个javascript片段连接到我需要转换为c ++的推送服务
<script src="//js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">
var pusher = new Pusher("cb65d0a7a72cd94adf1f");
var channel = pusher.subscribe("ticker.155");
channel.bind("message", function(data) {
//console.log(data);
var topbuy = data.trade.topbuy;
var topsell = data.trade.topsell;
console.log("Buy Price: ", topbuy.price,
"Buy Quantity:", topbuy.quantity),
console.log("Sell Price: ", topsell.price,
"Sell Quantity:", topsell.quantity);
});
</script>
我能够打开连接数据包以了解http://i.imgur.com/4iYIdLz.png发生的事情但我似乎无法建立联系。
socketclass.cpp
#include "socketclass.h"
socketClass::socketClass(QObject *parent) :
QObject(parent)
{
}
void socketClass::Test()
{
socket = new QTcpSocket(this);
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket,SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket,SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
qDebug() << "......Connecting";
socket->connectToHost("ws.pusherapp.com/app/cb65d0a7a72cd94adf1f?protocol=7&client=js&version=2.2.3&flash=false",80);
if(!socket->waitForConnected(1000))
{
qDebug() << "Error: " << socket->errorString();
}
}
void socketClass::connected()
{
//meat and potatoes goes here
qDebug() << "......Connected";
//socket->write("HEAD / HTTP/1.0\r\n\r\n\r\n");
socket->event();
}
void socketClass::disconnected()
{
qDebug() << "......Disconnected";
}
void socketClass::bytesWritten (qint64 bytes)
{
qDebug() << "We wrote: " << bytes;
}
void socketClass::readyRead()
{
qDebug() << "Reading..,,";
qDebug() << socket->readAll();
}
socketclass.h
#ifndef SOCKETCLASS_H
#define SOCKETCLASS_H
#include <QDebug>
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
class socketClass : public QObject
{
Q_OBJECT
public:
explicit socketClass(QObject *parent = 0);
void Test();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten (qint64 bytes);
void readyRead();
private:
QTcpSocket *socket;
};
#endif // SOCKETCLASS_H
的main.cpp
#include <QCoreApplication>
#include "socketclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
socketClass mTest;
mTest.Test();
return a.exec();
}
我应该使用c ++ websockets来模拟Javascript的正确连接参数是什么? 我只是想收到自动收报机数据并将其分配给一个对象或变量。
答案 0 :(得分:0)
我的Qt体验有点老了,但是从我看到你没有获得信号,因为你没有运行事件循环。你不应该使用waitForConnected
电话,你应该运行a.exec()
并让它去处理事件。唯一看起来很奇怪的是你拨打waitForConnected
,很可能会收到错误,但仍然会拨打a.exec()
但之后没有得到信号(或者你可能在失败后停止申请?)。