Emitter.prototype.emit = function(){ // packet var args = Array.prototype.slice.call(arguments); var packet = {}; packet.type = hasBin(args) ? parser.BINARY_EVENT : parser.EVENT; packet.data = args; // set namespace to packet if (this._flags.nsp) { packet.nsp = this._flags.nsp; delete this._flags.nsp; } else { packet.nsp = '/'; } // publish this.redis.publish(this.key, msgpack([packet, { rooms: this._rooms, flags: this._flags }])); // reset state this._rooms = []; this._flags = {}; return this; };
Mongo.prototype.broadcast = function (packet, opts, remote) { Adapter.prototype.broadcast.call(this, packet, opts); if (!remote) { channel.publish(key, { uid: uid, data: msgpack.encode([packet, opts]) }); } };
Fluent.prototype.write = function(tag, data){ if (this.tag) { tag = this.tag + "." + tag; } var time = (new Date()) / 1000, packedData = msgpack.encode([tag, time, data]); if (this._connected) { if (this._bufferQueue.length > 0) { this.flushBuffer(); } this._socket.write(packedData); } else { this._bufferedSize += packedData.length; if (this._bufferSize_ > this.maxBufferSize) { // emitの引数に Error オブジェクト渡すと、 // listenerいないと落とされるので、listenerがいるときだけemitする。 if (self.listeners('error').length > 0) { this.emit('error', new Error("Exceed max buffer size")); } this._bufferedSize -= packedData.length; } else { this._bufferQueue.push(packedData); } } };
var respCallback = function(err, result) { if (err && err.constructor == Error) { err = err.message; } var response = [1, msgId, err, result || null]; var packed = msgpack.encode(response); connection.write(packed); };
MsgPackConnection.prototype.send = function(message) { var frame = msgpack.encode(message); var header = new Buffer(4); header.writeUInt32BE(frame.length, 0); this.socket.write(header); this.socket.write(frame); };
function encode(packet, opts) { switch(codec) { case 'msgpack': return msgpack.encode([packet, opts]); default: return JSON.stringify([packet, opts]); } }
Client.prototype.notify = function() { var method = arguments[0]; var params = []; for (var i = 1, ii = arguments.length; i < ii; i++) { params.push(arguments[i]); } var obj = msgpack.encode([2, method, params]); this.socket.write(obj); };
beforeEach(function(){ // on send we need the routing frame on 0 frames = [ null, null, null, null, null, msgpack.encode({}), msgpack.encode({}), null, msgpack.encode({}) ]; spyOn(zmq, 'socket').andReturn(socketMock); spyOn(socketMock, 'send'); target.run(); });
beforeEach(function(){ var address = { sid: "test-zmq", sversion: "*", verb: "ping" }; frames = [ "identity", "ZSS:0.0", "REQ", "RID", msgpack.encode(address), msgpack.encode({}), null, msgpack.encode("data") ]; });
ZeroMQ.prototype.broadcast = function (packet, opts, remote) { Adapter.prototype.broadcast.call(this, packet, opts); if (!remote) { var channel = new Buffer(format('%s ', key), 'binary'); var payload = msgpack.encode([packet, opts]); var data = Buffer.concat([channel, payload]); debug('ZeroMQ#broadcast: send data length -> channel = %d, payload = %d, data = %d', channel.length, payload.length, data.length); pub.send(data); } };
function emit () { var args = Array.prototype.slice.call(arguments); var packet = { type: hasBin(args) ? parser.BINARY_EVENT : parser.EVENT, data: args, nsp: '/' }; var key = new Buffer('socket.io-zmq#emitter ', 'binary'); var payload = msgpack.encode([packet, { rooms: {}, flags: {} }]); emitter.send(Buffer.concat([key, payload])); }
Redis.prototype.broadcast = function(packet, opts){ Adapter.prototype.broadcast.call(this, packet, opts); var msg = msgpack.encode([uid, packet, opts]); if (opts.rooms) { var self = this; opts.rooms.forEach(function(room) { runCommand('publish', self.getChannelName(room), msg); }); } else { runCommand('publish', this.getChannelName(), msg); } };
Client.prototype.request = function() { var method = arguments[0]; var params = []; for (var i = 1, ii = arguments.length - 1; i < ii; i++) { params.push(arguments[i]); } var msgId = nextSequenceId++; var callback = arguments[arguments.length - 1]; this.requests[msgId] = callback; var obj = msgpack.encode([0, msgId, method, params]); this.socket.write(obj); };
return function (item) { var chunk = encode(item); var bytes = []; var length = chunk.length; while (length > 0x7f) { bytes.push(length & 0x7f | 0x80); length >>= 7; } bytes.push(length & 0x7f); emit(new Buffer(bytes)); emit(chunk); };
function send(clientId, message) { var socket = connections[clientId]; if(!socket) return -1; var frame = msgpack.encode(message); var header = new Buffer(4); header.writeUInt32BE(frame.length, 0); socket.write(header); socket.write(frame); }
MsgPackServer.prototype.send = function(clientId, message) { var socket = this.connections[clientId]; if(!socket) return -1; var frame = msgpack.encode(message); var header = new Buffer(4); header.writeUInt32BE(frame.length, 0); socket.write(header); socket.write(frame); };
Redis.prototype.broadcast = function (packet, opts, remote) { Adapter.prototype.broadcast.call(this, packet, opts); if (!remote) { const msg = msgpack.encode([uid, packet, opts]); if (opts.rooms) { opts.rooms.forEach(function (room) { const chnRoom = prefix + "#" + room; pub.publish(chnRoom, msg); }); } else { pub.publish(prefix, msg); } } };
Redis.prototype.broadcast = function(packet, opts, remote){ Adapter.prototype.broadcast.call(this, packet, opts); if (!remote) { var chn = prefix + '#' + packet.nsp + '#'; var msg = msgpack.encode([uid, packet, opts]); if (opts.rooms) { opts.rooms.forEach(function(room) { var chnRoom = chn + room + '#'; pub.publish(chnRoom, msg); }); } else { pub.publish(chn, msg); } } };
client.on('message', function(message, flags) { // flags.binary will be set if a binary message is received // flags.masked will be set if the message was masked var data = (flags.binary) ? msgpack.decode(message) : JSON.parse(message); ///console.log('business message:'+JSON.stringify(data)); data += ' reply by '+self.usrinfo.usrkey; try { client.send(msgpack.encode(data), {binary: true, mask: true}, function(err){ if (err) { console.log(err+',sendOpcMsg failed'); } }); } catch (e) { console.log(e+',sendOpcMsg failed immediately'); } });
redis.pubsub.prototype.publish = function(){ var finalContent = null; var intArgs = resolve4Arguments(arguments); var channel = intArgs[0]; var content = intArgs[1] var options = intArgs[2]; var cb = intArgs[3]; var encode = options.encode; if (encode){ finalContent = msgpack.encode(content); } else { finalContent = content; } this.db.publish(channel, finalContent, function(err, result){ cb(err, result); }); };
ws.on('open', function() { // var query = "start n=node(0) return n"; var query = "start n=node(0) match p=n-[r:KNOWS]->m return p,n,r,m,nodes(p) as nodes, rels(p) as rels,length(p) as length"; //var query = "start n=node(0) match n-[r:KNOWS]->m return r"; var data = msgpack.encode(query); var time=new Date(); var count=0; var max=100; var bytes = 0; var interval = 0; ws.on('message', function(message,flags) { //console.log(flags); var buffer=flags.buffer; // new Buffer(message); bytes += buffer.length; var decoder = new msgpack.Decoder(buffer); do { var value = decoder.parse(); // console.log('received',count, JSON.stringify(value)); } while(decoder.offset !== buffer.length); count++; if ((count % 100) == 0) console.log(count); if (count==max) { // clearInterval(interval); ws.close(); // usually it would keep running } else { ws.send(data,{binary:true,fin:true}); } }); ws.on('error',console.log); ws.on('close',function() { console.log("closing count",count," start ",time," end ",new Date(),new Date()-time," ms","bytes",bytes); }) ws.send(data,{binary:true,fin:true}); // for (var i=0;i<max;i++) { // // console.log(i); // ws.send(data,{binary:true,fin:true}); // } // interval = setInterval(function() { // if (count<max) return; // clearInterval(interval); // ws.close(); // usually it would keep running // } ,250); });
Emitter.prototype.emit = function(){ // packet var args = Array.prototype.slice.call(arguments); var packet = {}; packet.type = hasBin(args) ? parser.BINARY_EVENT : parser.EVENT; packet.data = args; // set namespace to packet if (this._flags.nsp) { packet.nsp = this._flags.nsp; delete this._flags.nsp; } else { packet.nsp = '/'; } debug('namespace: ', packet.nsp); // publish var msg = [packet, { rooms : this._rooms, flags : this._flags }]; debug('msg contents: ', msg); if(this.encode){ msg = msgpack.encode(msg).toString('hex'); debug('raw data: ', msg); } request({ url : this.base, method : 'POST', body : { channel : this.key, msg : msg }, json : true }, function(err){ // log errors if(err){ console.error('[socket.io-couchdb-emitter]: error sending message to couch: ', err); } }); // reset state this._rooms = []; this._flags = {}; return this; };
CouchDB.prototype.broadcast = function(packet, opts, remote){ debug('broadcasting data: ' + JSON.stringify(packet)); Adapter.prototype.broadcast.call(this, packet, opts); // add the message to the db if(!remote){ debug('broadcasting message: ', [packet, opts]); var msg = encode ? msgpack.encode([packet, opts]).toString('hex') : [packet, opts]; request({ url : base, method : 'POST', body : { channel : key, msg : msg }, json : true }); } };
function emit(event) { var out; // console.log("EVENT", event); if (event.type === "setup") { out = [1, event.id, event.parentId, event.name]; } else if (event.type === "before") { out = [2, event.id]; } else if (event.type === "after") { out = [3, event.id]; } else if (event.type === "error") { out = [4, event.id]; } else if (event.type === "root") { out = [5, event.id, event.name]; } var message = msgpack.encode(out); wss.broadcast(message); }
Emitter.prototype.emit = function () { var self = this; // packet var args = Array.prototype.slice.call(arguments); var packet = {}; packet.type = hasBin(args) ? parser.BINARY_EVENT : parser.EVENT; packet.data = args; // set namespace to packet if (this._flags.nsp) { packet.nsp = this._flags.nsp; delete this._flags.nsp; } else { packet.nsp = '/'; } var opts = { rooms: this._rooms, flags: this._flags }; var msg = msgpack.encode([uid, packet, opts]); // publish if (opts.rooms && opts.rooms.length) { opts.rooms.forEach((room) => { const chnRoom = this.prefix + "#" + room; self.redis.publish(chnRoom, msg); }); } else { this.redis.publish(this.prefix, msg); } // reset state this._rooms = []; this._flags = {}; return this; };
setInterval(function(){ socket.send(msgpack.encode('Hello, This is '+nmclnsA.usrinfo.usrkey+' over TURN.'), {binary: true, mask: true}); }, 2000);
BlockFile.create = function(filename, metaProps_, createcb){ if (typeof createcb == 'undefined' && typeof metaProps_ == 'function') { // if (arguments.length == 2) { createcb = metaProps_ metaProps_ = {} } assert( u.isPlainObject(metaProps_) , "metaProps argument is not a plain object") var metaProps = u.defaults( u.clone(metaProps_) , Props.defaultProps.metaProps()) Props.assertValidMetaProps(metaProps) // Construct the BlockFile metadata block // var mdBuf = new Buffer(MD_BLOCKSIZE) , crcValue, mode, fd, bf mdBuf.fill(0) //fill whole buffer with zeroes // 'number of segments' // var numSeg = 0 mdBuf.writeDoubleBE(numSeg, MD_MAP['number of segments']) // metaProps & appData // var metaPropsBuf = msgpack.encode(metaProps) , appDataBuf = msgpack.encode(null) , mdBufAvail = mdBuf.length - MD_MAP['END_FIXED'] , metaPropsOff = MD_MAP['END_FIXED'] , appDataOff = MD_MAP['END_FIXED'] + metaPropsBuf.length mdBuf.writeUInt16BE(metaPropsBuf.length, MD_MAP['metaPropsLen']) metaPropsBuf.copy(mdBuf, metaPropsOff) mdBufAvail -= metaPropsBuf.length if (mdBufAvail < appDataBuf.length) { createcb(new Error( format( "BlockFile.create: mdBufAvail < appDataBuf.length; %d < %d" , mdBufAvail, appDataBuf.length ) )) } mdBuf.writeUInt16BE(appDataBuf.length, MD_MAP['appDataLen']) appDataBuf.copy(mdBuf, appDataOff) mdBufAvail -= metaPropsBuf.length //not really necessary, but ... whatever! //MUST BE last write // file header checksum signCRC32(mdBuf, MD_MAP['crc32 value']) mode = 420 //Octal: 0644; octals not allowed in strict mode async.waterfall( [ function(cb) { //open in write & exclusive mode; ie errors out if file exists fs.open(filename, 'wx+', mode, cb) } // callback from fs.open is cb(err, fd) , function(fd_, cb) { fd = fd_ // write header primary fs.write( fd , mdBuf /*buffer*/ , 0 /*buffer offset*/ , mdBuf.length /*number of bytes to read*/ , MD_OFFSET_PRIMARY /*file position*/ , cb ) } // callback from fs.write is cb(err, written, buffer) , function(bytesWritten, buffer, cb) { // write header backup fs.write( fd , mdBuf /*buffer*/ , 0 /*buffer offset*/ , mdBuf.length /*number of bytes to read*/ , MD_OFFSET_SECONDARY /*file position*/ , cb ) } // callback from fs.write is cb(err, written, buffer) , function(written, buffer, cb) { fs.close(fd, cb) } // call back from fs.close is cb(err) , function(cb) { BlockFile.open(filename, metaProps, cb) } ], // callback from fs.close is cb(err) or error in waterfall createcb ) } //BockFile.create()
setInterval(function(){ socket.send(msgpack.encode('Hello, This Tom Zhou on TURN. :)'), {binary: true, mask: true}); }, 2000);
function query(query, cb) { var data = msgpack.encode(query); ws.send(data,{binary:true,fin:true},cb); // error callback }
write = function (item) { var serialized = msgpack.encode(item); output.write(uleb128(serialized.length)); return output.write(serialized); };