1
        socket.on('data', function(data, socket){
        console.log(socket);
        worker.handle(data, socket);

When I do this, the output is undefined. Why do I get that instead of socket data? This passes an (undefined) socket to my worker script, which then tries to write to that undefined socket, crashing the server.

1 Answer 1

2

Your callback is wrong. socket.on('data') takes a function with just one parameter -- the data. Your 2nd parameter is not valid and won't be called.

You should (IMHO) just be able to refer to your 'socket' variable within the context of the callback.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - just starting out with Node! Will accept when allowed - 2 min time limit.

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.