QtWebChannel question
Anton Kreuzkamp
akreuzkamp at web.de
Tue Mar 3 07:38:22 UTC 2015
Hi,
yes, QtWebChannel is supposed to do client-server-communication. Currently it
only works with a C++/Qt based server (which I use in a personal project). But
theoretically (and that's the goal after all) it's possible to do the
communication between a nodejs/php/JavaEE/... server and a qmlweb client using
QtWebChannel (that's possible, as soon as someone implements the server side
of the protocol for those server-technologies).
To use it on client side:
1. include qwebchannel.js into your index.html:
<script type="text/javascript" src="qmlweb/src/qwebchannel.js"></script>
2. instanciate a websocket and a webchannel in your js-code:
var socket = new WebSocket('example.com:8080/myWebSocketServer');
var myObj;
socket.onclose = function() {
console.log("web channel closed.");
};
socket.onerror = function(error) {
console.error("web channel error: " + error);
};
socket.onopen = function() {
new QWebChannel(socket, function(channel) {
// Stuff we do when the remote object arrives.
myObj = channel.objects.myObj;
myObj.someSignal.connect(function(someArg) {
//...
});
});
}
3. In Qml do whatever you want with it (you can just access your object using
myObj). But attention: You always have to handle the case, the object is not
yet there. Alternatively you can create the QML scene just when the object
arrived.
I plan to implement QMLEngine.registerSingleton(), though, which would then
wait for the channel to be ready, synchronously.
4. For the (C++/Qt-) server side you need (e.g. in your main()):
QWebChannel channel;
QWebSocketServer server("QWebChannel Standalone Example Server",
QWebSocketServer::NonSecureMode);
if (!server.listen(QHostAddress::Any, 10002)) {
qFatal("Failed to open web socket server.");
return 1;
}
MyObj myObj;
QObject::connect(&server, &QWebSocketServer::newConnection, [&] () {
channel.connectTo(new WebSocketTransport(server.nextPendingConnection()));
// Do some init stuff...
});
channel.registerObject("myObj", &myObj);
Cheers, Anton
On Monday 02 March 2015 21:00:47 you wrote:
> Anton, hi again!! ;-)
>
> I noted that you made some deeds on QtWebChannel thing.
>
> Could you please describe, how could it be used?
>
> I googled some info and found
> http://www.kdab.com/qt-webchannel-bridging-gap-cqml-web/
> but I cannot understand, if we are already in browser, how
> do you consider to use QtWebChannel?
>
> Can it be used for browser-server communication?
>
> e.g. can I load some qml objects in nodejs and communicate
> with browser using QtWebChannel?
>
> Best Regards,
> Pavel Vasev
--
Gruß auch an die NSA.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 230 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/qmlweb/attachments/20150303/ad1ce1af/attachment.sig>
More information about the QmlWeb
mailing list