Skip to content

Instantly share code, notes, and snippets.

@agustik
Created August 21, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustik/84a793be0a6b710bb4dedc3808029b91 to your computer and use it in GitHub Desktop.
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
// 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