Skip to content

using the same service on 2 protocols  #64

Description

@sancelot

For technical reasons, I would like to use the same service with two different procols
one with tcp (for python2 clients, where I don't have any json rpc websocket available)
and one with websocket , for web application .
I used different port for the tcp and websocket service

unfortunately, this does not seem to work, I can not work with the websocket service, but the tcp service is ok

I know I have to setup some mutexes in service, if I want to share the same fonctions between the two protocols.

here is my server code :

#include "ControlerServiceApi.h"
#include <jcon/json_rpc_tcp_server.h>
#include <jcon/json_rpc_websocket_server.h>
#include "jcon/json_rpc_file_logger.h"
#include <QCoreApplication>
#include <QUrl>

#include <ctime>
#include <iostream>
#include <memory>
#include <unistd.h>

ControlerService *service;

enum class SocketType {tcp, websocket};

jcon::JsonRpcServer* startServer(QObject* parent,
                                 SocketType socket_type = SocketType::tcp)
{
    int port;
    jcon::JsonRpcServer* rpc_server;
    if (socket_type == SocketType::tcp) {
        qDebug() << "Creating TCP server";
        auto logger = std::make_shared<jcon::JsonRpcFileLogger>("e:\\rpc_log.txt");
        rpc_server = new jcon::JsonRpcTcpServer(parent, logger);
        port = 6004;
    } else {
        qDebug() << "Creating WebSocket server";
        rpc_server = new jcon::JsonRpcWebSocketServer(parent);
        port = 6003;
    }
    rpc_server->registerServices({ service });
    rpc_server->listen(port);
    return rpc_server;
}



int main(int argc, char* argv[])
{

    QCoreApplication app(argc, argv);

    service = new ControlerService();

    auto server1 = startServer(nullptr, SocketType::websocket);
    // pour compatibilité python2, obligé de garder l acces en mode socket tcp
    auto server2 = startServer(nullptr, SocketType::tcp);

#ifdef _WIN32
	{
		std::string path;
        path.assign(tmpdir());
		path += "\\controler_service_ok";
		touch(path.c_str());
	}
#else
	int ret  __attribute__((unused))=system("touch /tmp/controler_service_ok");
#endif
    app.exec(); // <- here 
    delete server1;
    delete server2;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions