ndn-js-contrib
Version:
Reusable 'Classes' for Named Data Networking: NameTree, PIT, FIB, ContentStore, Interfaces, and Transports
1 lines • 1.67 kB
JavaScript
function DataChannelTransport(a){return Transport.call(this),this.connectionInfo=new DataChannelTransport.ConnectionInfo(a),this}var ElementReader=require("ndn-lib/js/encoding/element-reader.js").ElementReader,Transport=require("ndn-lib/js/transport/transport.js").Transport,debug={};debug.debug=require("debug")("DataChannelTransport"),DataChannelTransport.prototype=new Transport,DataChannelTransport.prototype.name="DataChannelTransport",DataChannelTransport.ConnectionInfo=function(a){Transport.ConnectionInfo.call(this),a.binaryType="arraybuffer",this.channel=a},DataChannelTransport.ConnectionInfo.prototype=new Transport.ConnectionInfo,DataChannelTransport.ConnectionInfo.prototype.name="DataChannelTransport.ConnectionInfo",DataChannelTransport.ConnectionInfo.prototype.getChannel=function(){return this.channel},DataChannelTransport.ConnectionInfo.prototype.equals=function(a){return null===a||void 0===a.port?!1:this.port===a.port},DataChannelTransport.prototype.connect=function(a,b,c,d){this.elementReader=new ElementReader(b);var e=this;a.getChannel().onmessage=function(a){if(a.data instanceof ArrayBuffer){var b=a.data,c=new Buffer(new Uint8Array(b));try{e.elementReader.onReceivedData(c)}catch(d){return void cdebug("onmessage exception: ",d)}}},a.getChannel().onopen=function(){c()},a.getChannel().onerror=function(){},a.getChannel().onclose=function(){e.dc=null,e.face.readyStatus=3,e.face.closeByTransport(),d()},"open"===a.getChannel().readyState&&c()},DataChannelTransport.prototype.send=function(a){"open"===this.connectionInfo.getChannel().readyState&&this.connectionInfo.getChannel().send(a.toArrayBuffer())},module.exports=DataChannelTransport;