Giter VIP home page Giter VIP logo

Comments (17)

aluisiora avatar aluisiora commented on July 1, 2024

Are you sure that the first one was disconnected?
Also, this seems like an issue with the logic used in your socket.io, where you are using the same event to send message for two distinct connection, you could try using rooms. But the way it is now, make sure you have disconnected from the previous router by looking at the logs of the router itself, maybe this line is never running:

if(status == true) {
    ...
} else {
    if (api) {
        const closing = api.rosApi.closing; // you don't have to check it yourself, the api already does this
        if (!closing) api.close(); // <- this line must run
     }
}

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

Unfortunately, I reviewed my code there is not mistake. I am sure that routeros does not close once I use routeros.close() even I used console.log(routeros.rosApi.host) after routeros.close(), it shows me server host name. I used also routeros.disconnect(), it's still remain give me server information.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I`ll try to get a lab running with 2 routeros instances, but I fail to see where the lib might be failing to disconnect.

Have you tried this?

if(status == true) {
    ...
} else {
    if (api) {
        api.close();
     }
}

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

Yes, api.close() and api.disconnect() does not work, server still remain shows me information after I use these commands.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I've setup a lab with 2 routeros using chr for testing, adapted your code for the setup and it worked just fine.

Here is a gif of it:
https://drive.google.com/open?id=1nIKIQqY65NxNkn1ZVH1ccJqUkxMNxZyf

Here is the code I used:

const { RouterOSClient } = require("routeros-client");

const serverConnection = (data) => {
    return new RouterOSClient({
        host: data.host,
        user: data.user,
        password: data.password,
        keepalive: true,
        timeout: 50
    });
};

let api;

const totalClients = (serverData, status) => {
    if (status == true) {
        api = serverConnection(serverData);
        api.connect().then(client => {
            console.log(`connected into ${serverData.host}`);

            client.menu("/ppp secret")
                .query({ interval: 2 })
                .stream('print', (err, data) => {
                    if (err) {
                        console.error(err);
                    }

                    if (data.length > 0) {
                        console.info(data.length);
                    }
                });
        });
    } else {
        if (api) {
            api.close().then(() => {
                console.log("disconnected");
            });
        }
    }
}

const server1 = {
    host: '192.168.0.110',
    user: 'admin',
    password: 'admin'
};

const server2 = {
    host: '192.168.0.111',
    user: 'admin',
    password: 'admin'
};

totalClients(server1, true);

setTimeout(() => {
    totalClients(server1, false);
    totalClients(server2, true);

    setTimeout(() => {
        totalClients(server2, false);
    }, 10 * 1000);

}, 10 * 1000);

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I've tested with just node-routeros and debugged some information, can confirm that, even if the api is disconnecting, the stream won't stop sending data.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I've issued an update to node-routeros that might fix it, try updating to see if that solves this issue.

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

Should I use node-routeros instead of routeros-client?

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

No, just do an npm update and check if the underlying node-routeros gets updated to 1.6.7.

node-routeros is a sub-package of routeros-client.

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

Actually, on my app I am using just routeros-client package. I update these package, but still same issue. This is my whole code:
`const RouterOSClient = require('routeros-client').RouterOSClient
require('dotenv').config()
let api

module.exports = (io) => {
io.on('connection', (socket) => {

    socket.on('StartGetInformation', (serverData, status) => {
        if(status == false) {
            if(api) {
                    api.close()
            }
        } else {
            api = new RouterOSClient({
                host: serverData.host,
                user: serverData.user,
                password: serverData.password,
                keepalive: true,
                timeout: 100
            })

            api.connect().then(client => {
                client.menu("/tool torch")
                    .where({interface: "WAN-1"})
                    .stream((err, packet) => {
                        if(packet.length > 0) {
                            const rxData = packet[0].rx / (1000 * 1000)
                            const rxDataRounded = Math.round(rxData)
                            const txData = packet[0].tx / (1000 * 1000)
                            const txDataRounded = Math.round(txData)
                            const data = {rx:  rxDataRounded, tx: txDataRounded}
                            io.emit('SendSpeedData', data)
                        }
                    })

                client.menu("/ppp active")
                    .query({ interval: 5 })
                    .stream('print', (err, data) => {
                        if(data.length > 0) {
                            io.emit('Clients-Active', data.length)
                        }
                    })
            
                client.menu("/ppp secret")
                    .query({ interval: 5 })
                    .stream('print', (err, data) => {if(data.length > 0) io.emit('Clients-Length', data.length)})
            }).catch(err => {
                console.log(err)
            })
        }
    })
})

}`

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I see, I may need to force an update through package.json then, routeros-client uses node-routeros under the hood.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I've updated routeros-client to v1.0.2 which should force node-routeros to update too. Could you try it again with those versions?

Edit: routeros-client should now be at version 1.1.0.

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

I do not know sometimes work perfectly, and sometimes shows me same issue.
Is it normal if I use api.close() to get inflrmation about server like host.
I use command: console.log(api.rosApi.host) after api.close(), these command shows me host name.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

I do not know sometimes work perfectly, and sometimes shows me same issue.
Is it normal if I use api.close() to get inflrmation about server like host.
I use command: console.log(api.rosApi.host) after api.close(), these command shows me host name.

It is, the routeros information will remain with the "api" object until you don't use it anymore and the garbage collector removes it from memory. This lib will reuse this information to do re-logins and etc.

from routeros-client.

aziz1117 avatar aziz1117 commented on July 1, 2024

Do you have any idea to remove these information and clear "api" object completely from memory.

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

To be honest, you shouldn't be using the "rosApi" property since it is private.

I really don't understand what is still the issue you are having, after version 1.1.0 of routeros-client and version 1.6.6 of node-routeros, in my lab with a sample of your code, it worked just fine.
Can you provide any logging information?

from routeros-client.

aluisiora avatar aluisiora commented on July 1, 2024

Closing this issue since there was no response from the OP.

from routeros-client.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.