Fix timeout on connections

This commit is contained in:
Cryptkeeper 2015-11-02 00:01:04 -06:00
parent dec1536c6c
commit 0cbfcf60d7
4 changed files with 11 additions and 7 deletions

1
.gitignore vendored

@ -1 +1,2 @@
node_modules/ node_modules/
*.log

2
app.js

@ -70,6 +70,8 @@ server.start(function() {
// Attach our listeners. // Attach our listeners.
client.on('disconnect', function(client) { client.on('disconnect', function(client) {
connectedClients -= 1; connectedClients -= 1;
logger.log('info', 'Client disconnected, total clients: %d', connectedClients);
}); });
}); });
}); });

@ -8,8 +8,8 @@ winston.add(winston.transports.File, {
winston.add(winston.transports.Console, { winston.add(winston.transports.Console, {
'timestamp': function() { 'timestamp': function() {
var date = new Date(); var date = new Date();
return date.toLocaleTimeString() + " " + date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear().toString().substring(2, 4); return date.toLocaleTimeString() + ' ' + date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear().toString().substring(2, 4);
}, },
'colorize': true 'colorize': true
}); });

@ -5,6 +5,12 @@ function pingMinecraftPC(host, port, timeout, callback) {
var client = new net.Socket(); var client = new net.Socket();
var milliseconds = (new Date).getTime(); var milliseconds = (new Date).getTime();
client.setTimeout(timeout, function() {
client.destroy();
callback(new Error('timeout'), null);
});
client.connect(port, host, function() { client.connect(port, host, function() {
// Write out handshake packet. // Write out handshake packet.
var handshakeBuffer = mcpc.createBuffer(); var handshakeBuffer = mcpc.createBuffer();
@ -69,11 +75,6 @@ function pingMinecraftPC(host, port, timeout, callback) {
client.on('error', function(err) { client.on('error', function(err) {
callback(err, null); callback(err, null);
}); });
// Make sure we don't go overtime.
setTimeout(function() {
client.end();
}, timeout);
} }
// Wraps our Buffer into another to fit the Minecraft protocol. // Wraps our Buffer into another to fit the Minecraft protocol.