diff --git a/lib/browser.js b/lib/browser.js index 643aba5..7c31377 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -369,7 +369,7 @@ function map(ary, each, done) { * @api public */ -exports.decodePayload = function (data, binaryType, callback) { +exports.decodePayload = function (data, binaryType, utf8decode, callback) { if (typeof data !== 'string') { return exports.decodePayloadAsBinary(data, binaryType, callback); } @@ -379,12 +379,24 @@ exports.decodePayload = function (data, binaryType, callback) { binaryType = null; } + if (typeof utf8decode === 'function') { + callback = utf8decode; + utf8decode = null; + } + var packet; if (data === '') { // parser error - ignoring payload return callback(err, 0, 1); } + if (utf8decode) { + data = tryDecode(data); + if (data === false) { + return callback(err, 0, 1); + } + } + var length = '', n, msg; for (var i = 0, l = data.length; i < l; i++) { diff --git a/lib/index.js b/lib/index.js index b54bbb5..9036816 100644 --- a/lib/index.js +++ b/lib/index.js @@ -265,7 +265,7 @@ function map(ary, each, done) { * @api public */ -exports.decodePayload = function (data, binaryType, callback) { +exports.decodePayload = function (data, binaryType, utf8decode, callback) { if (typeof data !== 'string') { return exports.decodePayloadAsBinary(data, binaryType, callback); } @@ -275,11 +275,23 @@ exports.decodePayload = function (data, binaryType, callback) { binaryType = null; } + if (typeof utf8decode === 'function') { + callback = utf8decode; + utf8decode = null; + } + if (data === '') { // parser error - ignoring payload return callback(err, 0, 1); } + if (utf8decode) { + data = tryDecode(data); + if (data === false) { + return callback(err, 0, 1); + } + } + var length = '', n, msg, packet; for (var i = 0, l = data.length; i < l; i++) {