diff --git a/lib/handlers/connect.js b/lib/handlers/connect.js index fa5f42e9..d408d26e 100644 --- a/lib/handlers/connect.js +++ b/lib/handlers/connect.js @@ -22,6 +22,15 @@ var connectActions = [ emptyQueue ] +var errorMessages = [ + '', + 'unacceptable protocol version', + 'identifier rejected', + 'Server unavailable', + 'bad user name or password', + 'not authorized' +] + function handleConnect (client, packet, done) { client.connected = true client.clean = packet.clean @@ -60,28 +69,35 @@ function authenticate (arg, done) { negate) function negate (err, successful) { + var errCode if (!err && successful) { client.broker.registerClient(client) return done() } else if (err) { if (err.returnCode && (err.returnCode >= 1 && err.returnCode <= 3)) { + errCode = err.returnCode write(client, { cmd: 'connack', returnCode: err.returnCode }, client.close.bind(client, done)) } else { // If errorCode is 4 or not a number + errCode = 4 write(client, { cmd: 'connack', returnCode: 4 }, client.close.bind(client, done)) } } else { + errCode = 5 write(client, { cmd: 'connack', returnCode: 5 }, client.close.bind(client, done)) } + var error = new Error(errorMessages[errCode]) + error.errorCode = errCode + client.broker.emit('clientError', client, error) } }