Created
August 21, 2017 16:17
-
-
Save agustik/84a793be0a6b710bb4dedc3808029b91 to your computer and use it in GitHub Desktop.
socket.send is async, but does not provide callback, the data will just buffer up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket | |
// Allows you to add callback to socket.send.. | |
// Uses Async.js but you could change that. | |
socket.sendAsync = function sendAsync(payload, callback){ | |
callback = callback || function (){}; | |
var _self = this; | |
_self.send(payload); | |
if (_self.bufferedAmount === 0) return callback(); | |
async.whilst(function (){ | |
return (_self.bufferedAmount > 0); | |
}, function (fn){ | |
return async.nextTick(fn); | |
}, function done(){ | |
return callback(); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment