用于Kaazing Gateway的qt C ++ WebSocket聊天客户端

时间:2016-05-08 21:13:47

标签: c++ qt kaazing

我在qt c ++中为Kaazing Gateway编写聊天客户端。我用的是QTcpSocket。我调用connectToHost并且连接正常。但是当我调用socket写入功能时,连接断开。怎么了?如何向kaazing服务器写消息?

//connect to server
QTcpSocket _sok = new QTcpSocket(this);
connect(_sok, SIGNAL(connected()), this, SLOT(onSokConnected()));
connect(_sok, SIGNAL(disconnected()), this, SLOT(onSokDisconnected()));

_sok->connectToHost("localhost", 8000); //after this line run onSokConnected()


// write message
    QByteArray  arrBlock;
    QDataStream out(&arrBlock, QIODevice::WriteOnly);
    out.setVersion( QDataStream::Qt_4_5 );
    out << quint16(0) <<sometext;

    out.device()->seek(0);
    out << quint16(arrBlock.size() - sizeof(quint16));

    _sok->write(arrBlock);  // after this line run onSokDisconnected()
    _sok->flush();

1 个答案:

答案 0 :(得分:0)

  • 您必须使用QT Websocket库,您可以找到一个示例here
  • 第二,如果您已经从kaazing.com/download下载了网关,它已经配置了echo服务,您需要将该URI(ws:// localhost:8000 / echo)放在main.cpp中。 / LI>

如果您按照这两个步骤操作,您应该可以毫无问题地连接到网关!