2

QWebSocket 与 C++ 的连接有问题。

QWebSocket *mWebSocket = new QWebSocket();
connect(mWebSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(mWebSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(mWebSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));

QNetworkRequest lRequest(QUrl("wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages"));
lRequest.setRawHeader("Predix-Zone-Id", <my unique id>);
lRequest.setRawHeader("Authorization", <some token>);

mWebSocket->open(lRequest);

我收到 3 个错误,然后断开连接,但从不连接。

调用槽:错误
QAbstractSocket::RemoteHostClosedError
调用槽:错误
QAbstractSocket::ConnectionRefusedError
调用槽:错误
QAbstractSocket::RemoteHostClosedError
调用槽:断开连接

当我在令牌中输入一个小错字(以测试身份验证是否正常)时,我开始接收只有 QAbstractSocket::ConnectionRefusedError 错误。

最有趣的部分是我已经用python实现了websocket连接并且效果很好,所以问题不应该来自websocket服务器部分,或者请求头设置:

import websocket
import thread
import time

def on_message(ws, message):
print(message)

def on_error(ws, error):
print(error)

def on_close(ws):
print("### closed ###")

def on_open(ws):
def run(*args):
    for i in range(3):
        time.sleep(1)
        #ws.send('{messageId: 1453338376222,body: [{name: Compressor-2017:CompressionRatio,datapoints: [[1453338376222,10,3],[1453338377222,10,1]],attributes: {host: server1,customer: Acme}}]}')
    time.sleep(1)
    #ws.close()
    print("thread terminating...")
thread.start_new_thread(run, ())


if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages",
                          on_message = on_message,
                          on_error = on_error,
                          on_close = on_close,
                          header     = {'Predix-Zone-Id:my unique id', 'Authorization:token'}
                          )
ws.on_open = on_open
ws.run_forever()

这个 websocket 连接是我的 c++ sdk 的一部分,所以我需要用 c++ 实现它。您对我在 C++ 代码中遗漏的内容有任何想法吗?

4

0 回答 0