[Interest] Problems with QWebSocketServer

Adrian Jäkel aj at elane2k.com
Wed Jun 22 16:28:21 CEST 2016


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




More information about the Interest mailing list