UNPKG

xcraft-axon

Version:

High-level messaging & socket patterns implemented in pure js

61 lines (46 loc) 960 B
/** * Module dependencies. */ var Socket = require('./sock'); var net = require('net'); /** * Expose `PubSocket`. */ module.exports = PubSocket; /** * Initialize a new `PubSocket`. * * @api private */ function PubSocket() { Socket.call(this); } /** * Inherits from `Socket.prototype`. */ PubSocket.prototype.__proto__ = Socket.prototype; /** * Send `msg` to all established peers. * * @param {Mixed} msg * @api public */ PubSocket.prototype.send = function(...args){ var socks = this.socks; var socket; if (args[args.length - 1] instanceof net.Socket) socket = args.pop(); var len = socket ? 1 : socks.length; var sock = socket; if (!len) return this; var buf = this.pack(args); for (var i = 0; i < len; i++) { if (!socket) sock = socks[i]; if (sock.writable) { const flushed = sock.write(buf); if (!flushed) { this.emit('socket need drain', sock); } } } return this; };