[Interest] Problems with QWebSocketServer

André Hartmann andre.hartmann at iseg-hv.de
Wed Jun 22 16:42:42 CEST 2016


Hi,

 > void MainWindow::buttonClicked()
 > {
 > // uncomment this for not working websocketserver in mainwindow
 > //  QWebSocketServer server(QStringLiteral("QWebSocketServer"),
 > QWebSocketServer::NonSecureMode);
 > //  if (!server.listen(QHostAddress::LocalHost, 12345)) {
 > //    qFatal("Failed to open web socket server.");
 > //  }
 > }


Your'e creating the server in the stackframe of 
MainWindow::buttonClicked(). As soon as the function is leaved, the 
stackframe is cleared and the destructor of QWebSocketServer is called.

Solution: add a member variable QWebSocketServer *m_server and allocate 
memory for it in the buttonClicked():

m_server = new QWebSocketServer(QStringLiteral("QWebSocketServer"),
 > QWebSocketServer::NonSecureMode);

Best regards,
André

PS: I know this problem because I had it same way with QTcpServer :)

Am 22.06.2016 um 16:28 schrieb Adrian Jäkel:
> Hi,
>
> i'm having problems starting a QWebSocketServer from within a
> QMainWindow. Perhaps i'm missing something obvious?
>
> When starting it from the main.cpp it works correctly and i can connect
> to it from my sample html page. But inside a QMainWindow e.g. after
> clicking a button my html page refuses to connect with "Error in
> connection establishment: net::ERR_CONNECTION_REFUSED"
>
> Below is a small sample app. Just un/comment accordingly when testing
> both cases.
>
> Regards,
> A. Jäkel
>
> main.cpp
>
> #include <QApplication>
> #include <QMainWindow>
> #include <QWebSocketServer>
>
> #include "mainwindow.h"
>
> int main(int argc, char** argv)
> {
>      QApplication app(argc, argv);
>
>      // comment the following lines when trying websocketserver from
> mainwindow
>      QWebSocketServer server(QStringLiteral("QWebSocketServer"),
> QWebSocketServer::NonSecureMode);
>      if (!server.listen(QHostAddress::LocalHost, 12345)) {
>          qFatal("Failed to open web socket server.");
>      }
>
>      MainWindow w;
>      w.show();
>
>      return app.exec();
> }
>
> mainwindow.cpp
>
> #include "mainwindow.h"
>
> #include <QWebSocketServer>
> #include <QPushButton>
>
> MainWindow::MainWindow(QWidget *parent) :
>    QMainWindow(parent)
> {
> // uncomment this for not working websocketserver in mainwindow
> //  QPushButton *button = new QPushButton("start server");
> //  setCentralWidget(button);
> //  connect(button, &QPushButton::clicked, this,
> &MainWindow::buttonClicked);
> }
>
> void MainWindow::buttonClicked()
> {
> // uncomment this for not working websocketserver in mainwindow
> //  QWebSocketServer server(QStringLiteral("QWebSocketServer"),
> QWebSocketServer::NonSecureMode);
> //  if (!server.listen(QHostAddress::LocalHost, 12345)) {
> //    qFatal("Failed to open web socket server.");
> //  }
> }
>
> mainwindow.h
>
> #ifndef MAINWINDOW_H
> #define MAINWINDOW_H
>
> #include <QMainWindow>
>
> class MainWindow : public QMainWindow
> {
>    Q_OBJECT
>
> public:
>    explicit MainWindow(QWidget *parent = 0);
>    ~MainWindow() {}
>
>    void buttonClicked();
> };
>
> #endif // MAINWINDOW_H
>
>
> index.html
>
> <html>
>      <head>
>          <script type="text/javascript">
>              window.onload = function() {
>                  var baseUrl = "ws://localhost:12345";
>
>                  console.info("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.onmessage = function(e)
>                  {
>                      console.info("message: " + e.data);
>                  };
>                  socket.onopen = function()
>                  {
>                      console.warn("WebSocket connected.");
>                  }
>              }
>          </script>
>      </head>
>      <body>
>      </body>
> </html>
>
> .pro file
> QT += gui widgets websockets
>
> SOURCES += \
>      main.cpp \
>      mainwindow.cpp
>
> HEADERS += \
>      mainwindow.h
>
> DISTFILES += \
>      index.html
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list