简体   繁体   English

Qt WebSockets连接到Pusher服务

[英]Qt websockets to connect to Pusher service

I have a javascript snippet that connects to a pusher service that I need to convert to c++ 我有一个连接到推送服务的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>

I was able to bust open the connection packet to get a little idea of what is going on http://i.imgur.com/4iYIdLz.png But I can't seem to get a connection. 我能够关闭连接数据包,以大致了解http://i.imgur.com/4iYIdLz.png发生了什么,但似乎无法建立连接。

socketclass.cpp 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 main.cpp

#include <QCoreApplication>
#include "socketclass.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    socketClass mTest;
    mTest.Test();

    return a.exec();
}

What are the proper connection parameters I should use with c++ websockets to emulate the Javascript? 我应该将c ++ websockets用于模拟Java的正确连接参数是什么? I am just trying to receive the ticker data and assign it to an object or variable. 我只是想接收代码数据并将其分配给对象或变量。

My Qt experience is a bit old, but from what I see you won't get the signals since you don't have an event loop running. 我的Qt经验有点老,但是从我的角度来看,由于没有运行事件循环,因此您不会得到信号。 You should not use the waitForConnected call, you should either run the a.exec() and let take it care of pumping the events. 您不应该使用waitForConnected调用,您应该运行a.exec()并注意泵送事件。 The only thing that seems weird is that you call the waitForConnected , most likely get an an error but still call a.exec() but don't get the signals after that (or maybe you are stopping the application after the failure?). 唯一看起来很奇怪的是,您调用了waitForConnected ,很可能会出错,但仍然调用a.exec()但此后没有得到信号(或者您可能在失败后停止应用程序?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM