等待套接字的响应,然后使用rabbitmq发送

时间:2019-07-09 06:17:40

标签: node.js sockets rabbitmq asyncsocket

我有5台服务器,它们通过net.Socket()连接到它们,并侦听来自每台服务器的数据。我将所有客户端存储在一个数组中。代码如下:

 for (let controllerAndDoors of controllersAndDoors) {

    let client = new net.Socket();  

    // add the client to the clients array
    clients.push(client);

    client.connect(controllerAndDoors.controller.Port, controllerAndDoors.controller.Ip, function () {
        console.log('\x1b[32m%s\x1b[0m', `Connected to controller: ${client.remoteAddress}:${client.remotePort}`);
    });

    client.on('data', function (data) {
        var date = new Date().toISOString();
        console.log(`${date}: Received message from: ${client.remoteAddress}:${client.remotePort}`);
        actionHandler.checkMessageFromController(client, data, controllerAndDoors);
    });

    client.on('timeout', function () {
        console.log('Client connection timeout');
    });

    client.on('error', function (err) {
        console.error('\x1b[31m%s\x1b[0m', err);

        // try to reconnect
        if (err.code === "ECONNRESET" || "ETIMEDOUT") {
            client.connect(controllerAndDoors.controller.Port, controllerAndDoors.controller.Ip, function () {
                console.log('\x1b[32m%s\x1b[0m', `Connected to controller: ${client.remoteAddress}:${client.remotePort}`);
            });
        }
    });

    client.on('end', function () {
        console.log(`Disconnected from controller`);
    });

    client.on('close', function () {
        console.log(`Connection closed`);
    });

现在我也有一个运行在同一程序上的Rabbitmq服务器。他的工作是听客户的消息。每个消息都需要写入套接字,并等待套接字的响应。问题是我需要等待响应并将其发送回Rabbitmq。如何等待每条消息的响应?同样,client.on('data',...)方法获取所有消息。我试图用事件发射器实现某些东西,但是没有结果...

0 个答案:

没有答案