0

As I'm building my own http module relying on net module, I want to set a timeout for every new socket. I build the following:

server = net.createServer(function (socket) {
          if(server.timeout==null){
                server.timeout=120000;
                if(server.timeoutCallBack==null){
                    server.timeoutCallBack=function(){socket.destroy();};
                }
            }
          socket.setTimeout(server.timeout,server.timeoutCallBack());

so if the user of my server, set a new timeout duration , it will use it. if not, it has a default value of 2 minutes.

My problem is that my server doesn't serve the first connection, just the second. I mean, for the first http message it gets, it doesn't invoke my :

            socket.on("data", function (data) {
            });

1 Answer 1

4

The thing you passed to socket.setTimeout as your callback is the result of server.timeoutCallBack(), which is undefined. Instead of calling timeoutCallBack 2 minutes after that line, you called it on that line and setTimeout of nothing.

You should pass a reference to a function to setTimeout instead:

socket.setTimeout(server.timeout,server.timeoutCallBack);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.