can't conduct the same code in visual studio 2019
-
Hi, everybody.
Here is the thing.
Recently, I have been developing a simple application that can transfer data from c++ to javascript files. So I found a way using websocket. I check the example code in QT, which is standalone example that creates a dialog between html and an qt app. thus, I am using the same method to transfer data. However, I found the same code, which can be conducted successfully on qt creator, can't perform correctly on visual studio 2019. The code I use is :#include "QtGuiApplication1.h" #include <QtWidgets/QApplication> int main(int argc, char** argv) { QApplication app(argc, argv); QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode); if (!server.listen(QHostAddress::LocalHost, 12445)) { qFatal("Failed to open web socket server."); return 1; } WebSocketClientWrapper clientWrapper(&server); QWebChannel webChannel; QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected, &webChannel, &QWebChannel::connectTo); return app.exec(); }
and the html code is
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="./qwebchannel.js"></script> <script type="text/javascript"> //BEGIN SETUP function output(message) { var output = document.getElementById("output"); output.innerHTML = output.innerHTML + message + "\n"; } window.onload = function() { if (location.search != "") var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); else var baseUrl = "ws://localhost:12445"; output("Connecting to WebSocket server at " + baseUrl + "."); var socket = new WebSocket(baseUrl); socket.onclose = function() { console.error("web channel closed"); }; socket.onerror = function(error) { console.error("web channel error: " + error); }; socket.onopen = function() { output("WebSocket connected, setting up QWebChannel."); new QWebChannel(socket, function(channel) { // make core object accessible globally var webobj = channel.objects.webobj; //var webobj = channel.objects.webobj; //window.webobj = channel.objects.webobj; console.log(webobj); console.log(webobj.jsonData.y); x.innerHTML = "x:" + "3"; webobj.dataChanged.connect(function (arg) { x.innerHTML = "x:" + "3"; y.innerHTML = "y:" + arg.y.toFixed(4); console.log("1111"); }); //console.log(channel.objects); document.getElementById("send").onclick = function() { var input = document.getElementById("input"); var text = input.value; if (!text) { return; } output("Sent message: " + text); input.value = ""; // core.receiveText(text); } // console.log(core.sendText); // core.sendText.connect(function(message) { // output("Received message: " + message); // } // ); // core.receiveText("Client connected, ready to send/receive messages!"); output("Connected to WebChannel, ready to send/receive messages!"); }); } } //END SETUP </script> <style type="text/css"> html { height: 100%; width: 100%; } #input { width: 400px; margin: 0 10px 0 0; } #send { width: 90px; margin: 0; } #output { width: 500px; height: 300px; } </style> <p id="x">x:</p> <p id="y">y:</p> </head> <body> <textarea id="output"></textarea><br /> <input id="input" /><input type="submit" id="send" value="Send" onclick="javascript:click();" /> </body> </html>
when I run the code on qt creator, I found the html could trigger the socket.onopen signals. However, when I run the same code on visual studio 2019, I found that the socket.onopen signals were never triggered.
I wonder there will be any thoughts can help me.
Thanks in advance!