Thursday 4 May 2017

Call function from outside

I am using this code to create a server that listens on a specific port and ip

function notifier() {

        var net = require('net');
        var host = 'x.x.x.x';
        var port = xxxx;

        var server = net.createServer(function (socket) {
            socket.on('data', function (data) {
                console.log("data " + data.toString());
            });

            socket.on('close', function (data) {
                console.log('closed');
            });

            socket.on('error', function (err) {
                console.log(err);
            });

        });

        function startServer() {
            server.listen(port, host, function () {
                console.log('started');
            });
        }

        function stopServer() {
            server.close(function () {
                console.log('stopped');
            });
        }

        startServer();
    }

I have also created a startServer and stopServer function.

If I call the function notifier() then the server starts but I cant figure out how I should call the stopServer function outside the function notifier()

Maybe I have made a huge mistake here and that I cant call it elsewhere, then maybe someone could point me in the right direction how it should be done correct.



via Kristoffer Isaksson

No comments:

Post a Comment