Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. How can a QWebSocketServer identify messages from multiple HTML clients and answer each separately?
Forum Updated to NodeBB v4.3 + New Features

How can a QWebSocketServer identify messages from multiple HTML clients and answer each separately?

Scheduled Pinned Locked Moved Solved QtWebEngine
3 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lingfa
    wrote on last edited by
    #1

    Refer to "Qt WebChannel Standalone Example"
    Step 1: setup the QWebSocketServer

        m_server = new QWebSocketServer("WebSocket app", QWebSocketServer::NonSecureMode, this);
        if (!m_server->listen(QHostAddress::LocalHost, 12345)) {
            qFatal("Failed to open web socket server, probabaly caused by an existing instance!");
            return;
        }
    

    Step 2: setup the channel

        m_channel = new QWebChannel(this);
    
        // setup the core and publish it to the QWebChannel
        m_channel->registerObject(QStringLiteral("core"), m_jsHelper);
        connect(m_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
    

    Step 3: handle incoming socket

    void MainWindow::newConnection()
    {
        QWebSocket *socket = m_server->nextPendingConnection();
        QWebChannelAbstractTransport *t = new WebSocketTransport(socket);
        m_channel->connectTo(t);
        connect(t, SIGNAL(messageReceived(QJsonObject,QWebChannelAbstractTransport*)),
                this, SLOT(messageReceived(QJsonObject,QWebChannelAbstractTransport*)));
    }
    

    Step 4: open two browsers, both load same index.html, which contains a connection from C++ signal trigger to a JavaScript function:

                            core.sendText.connect(function(message) {// sendText is a C++ signal
                                output("Received message: " + message);
                            });
    

    In this way, one server emits one sendText message which will display in multiple browser clients.
    My question is each client can send its own question to the server, is there a way the server can answer each client separately and only?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lingfa
      wrote on last edited by Lingfa
      #2

      Here is source code repository

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lingfa
        wrote on last edited by Lingfa
        #3

        Solved by 1) when a new client comes in, store WebSocketTransport in a list. 2) upon WebSocketTransport::textMessageReceived() trigger MainWindow::messageReceived() where indexing m_currentTransport. 3) Instead of sentText() use m_currentTransport->sendPlainTextMessage(text); to reply.

        Source code detail: git clone https://Lingfa@bitbucket.org/Lingfa/htmlchat.git

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved