有沒有一個用於Qt的WebSockets?是否有一個用於Qt的WebSockets?
13
A
回答
1
QtWebKit內部的QWebView支持使用Web套接字(來自HTML5標準)。我已經多次使用它們,沒有任何問題。
+4
你可以分享源代碼讓我們開始新手嗎? – BRampersad 2011-08-20 18:55:41
3
你也可以看看QtWebSockets。 QtWebSockets既可以用於客戶端和服務器應用程序,也可以通過Autobahn測試套件。
0
我已經創建了一個例子。下面是代碼:
inspection_server.hpp:
#ifndef __INSPECTION_SERVER_HPP__
#define __INSPECTION_SERVER_HPP__
#include <QWebSocketServer>
#include <QWebSocket>
#include <QObject>
#include <iostream>
#include <memory>
class InspectionServer;
typedef std::shared_ptr<QWebSocketServer> QWebSocketServerPtr;
typedef std::shared_ptr<QWebSocket> QWebSocketPtr;
typedef std::shared_ptr<InspectionServer> InspectionServerPtr;
class InspectionServer: public QObject
{
Q_OBJECT
QWebSocketServerPtr websocketServer;
QList<QWebSocketPtr> clients;
public:
InspectionServer(uint16_t port);
signals:
void closed();
private slots:
void onNewConnection();
void processTextMessage(const QString& message);
void socketDisconnected();
};
#endif
inspection_server.cpp:
#include "inspection_server.hpp"
#include <QDebug>
InspectionServer::InspectionServer(uint16_t port)
: websocketServer(new QWebSocketServer(QStringLiteral("Inspection server"), QWebSocketServer::NonSecureMode))
{
if(this->websocketServer->listen(QHostAddress::Any, port))
{
QObject::connect(websocketServer.get(), SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
else
{
throw std::runtime_error("InspectionServer: failed to listen");
}
}
void InspectionServer::onNewConnection()
{
qInfo() << "InspectionServer::onNewConnection";
QWebSocketPtr socket(this->websocketServer->nextPendingConnection());
QObject::connect(socket.get(), SIGNAL(textMessageReceived(const QString&)), this, SLOT(processTextMessage(const QString&)));
QObject::connect(socket.get(), SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
this->clients.append(socket);
}
void InspectionServer::processTextMessage(const QString& message)
{
qInfo() << "InspectionServer::processTextMessage: " << message;
}
void InspectionServer::socketDisconnected()
{
qInfo() << "InspectionServer::socketDisconnected";
}
main.cpp中:
#include "inspection_server.hpp"
#include <QCoreApplication>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
InspectionServer server(1234);
return app.exec();
}
這裏是如何測試中您的網絡瀏覽器:
<html>
<body>
<button onClick="initWebSocket();">Connect</button>
<br>
<input type="text" id="messageInput">
<button onClick="sendMessage();">Send message</button>
</body>
</html>
<script type="text/javascript">
var websocket = null;
function initWebSocket()
{
websocket = new WebSocket("ws://localhost:1234");
}
function sendMessage()
{
websocket.send(document.getElementById("messageInput").value);
}
</script>
這是一個很好的鏈接以供參考: http://code.qt.io/cgit/qt/qtwebsockets.git/tree/examples/websockets/echoserver
相關問題
- 1. websockets是否適用於SPDY?
- 2. 是否有一個用於wxpython的Windowlicker?
- 3. Qt Builder是否有一個用於編輯工具欄的內置工具?
- 4. 是否有開源的WebSockets(JavaScript)XMPP庫?
- 5. 是否有HTML5 WebSockets的Java-applet實現?
- 6. 是否有用於驗證QT端口的測試套件?
- 7. Android 3.0是否支持WebSockets?
- 8. RhinoJS是否支持websockets API?
- 9. 調用qt udpsocket flush是否有用?
- 10. 是否有一個ColdFusion相當於phpinfo()
- 11. 是否有一個JProfiler等效於.NET?
- 12. 是否有一個Monoid相當於Bifunctor?
- 13. qt是否帶有gnu make?
- 14. Qt是否有C接口?
- 15. 是否有Qt Jambi叉?
- 16. .NET是否有一個內置的IEnumerable用於多個集合?
- 17. 是否有開源的聊天庫使用websockets
- 18. PubNub是否使用WebSockets和/或XMPP?
- 19. Websockets是否適合使用PHP?
- 20. 如何在沒有webSockets的情況下使用QT WebChannel
- 21. 是否有人使用valgrind和Qt?
- 22. 是否有一個易於使用的Ruby的FTP庫?
- 23. 是否有一個用於ASP.NET的.browser文件的存儲庫?
- 24. 是否有一個用於Python的GUI生成器的IDE?
- 25. 是否有一個基於AJAX的基於網絡的Junit TestRunner?
- 26. Websockets:所有用戶的一個頻道或每個用戶的一個頻道?
- 27. 是否有一個適用於.NET Regex的Alpha匹配器?
- 28. 是否有一個用於在Scala中創建GUI的GUI?
- 29. 是否有一個gui設計器,用於Eclipse中的wxwidgets?
- 30. 是否有一個用於node.js的阻塞redis庫?
@charles:請不要刪除相關的變量,如果你沒有更多的相關性。另外,我們現在丟失了所有的tagwiki信息。下次請多加小心。 Qt和websockets現在已經成爲了Qt本身的一部分,這意味着QtWebsockets已經很多了。即使不是這樣,也可以根據相關答案添加標籤來進行元標記討論。 – lpapp 2014-06-05 14:32:18
@FinalContest,標籤是*問題*,而不是答案。這是**不是**關於答案中提到的特定庫的問題,這是關於Qt中的websockets的問題。 – Charles 2014-06-05 17:05:11
@FinalContest,我不是說這個標籤不應該存在,我只是說** **這個問題只有**這個標籤是不合適的。無論如何,這是一個圖書館推薦問題,因此是無關緊要的。還有[至少一些](http://stackoverflow.com/search?q=qtwebsocket)標籤可能和*可能應該存在的其他問題。編輯發生在很久以前,我不記得他們當時是否存在。 – Charles 2014-06-05 17:09:02