1

My desktop application needs to communicate with my chrome web extension. As suggested in several posts on SO, I want to do this via a local server. However, I am new to web development and have no server programming experience.

Now I do not know how to establish the connection between server (localhost) and client (web extension). The QLocalServer I am trying to use as local server only needs a server name like "MyLocalServer". If the client would also be made with Qt, that name would be sufficient to know. But the web extension implementation idea is to use javascript and a XMLHttpRequest. I am concerned about two things with the XMLHttpRequest:

  • How to connect to the local server when I only know its name? I do not know the port and all I see on the internet is like

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET",'http://localhost:8081/sample.xml',true);
    xmlhttp.send();
    

    where you need to know the port number.

  • Due to the same-origin policy, I am not sure if I can do it this way at all. Does it make a difference, if I do not need access to a file (like in the url above /sample.xml? I only want to communicate via streams. There is no need to access saved files on the harddrive.

If it helps, here is my first idea of the local Qt server:

server.h:

class QLocalServer;
class Server : public QObject
{
    Q_OBJECT
public:
    explicit Server(QObject *parent = 0);
private slots:
    void onNewConnection();
    void sendResponse();
private:
    QLocalServer* server_;
};

server.cpp:

Server::Server(QObject *parent) :
    QObject(parent),
    server_(new QLocalServer)
{
    server_->listen("MyLocalServer");
    connect(server_,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
}

void Server::onNewConnection()
{
    qDebug()<<"New connection.";
}

void Server::sendResponse()
{
    qDebug()<<"Sending response...";
}

EDIT:

I am not limited to use QLocalServer, if it works, I could also use QTcpServer. Then I'd have an IP address and port number to use with the XMLHttpRequest, but unfortunately I could not make it work, either. I found this post from 2013 that is about a similar topic, and it is suggested to either use XMLHttpRequest (as I tried above) or to use websockets.

Community
  • 1
  • 1
user2366975
  • 4,350
  • 9
  • 47
  • 87

0 Answers0