Pisasmallframeworkusedtocreatebrowser-to-browsernetworks(asopposedtojustaconnection).WithP,youcan:
ConnecttootherbrowsersusingasimpleWebSocketserver.Connecttootherbrowsersusingyourestablishedconnectionstootherbrowsers.ThisiswhatmakesPunique:itallowsfortransitiveconnectionsacrosspeers,allowingeasycreationofmeshnetworks.Afteraconnectionisestablishedthemiddlemanisnolongernecessary–noproxiesareinvolved.
Thisismadepossiblebyanunstableandyoungtechnology--WebRTC.Currently,onlyChromeandFirefoxsupportthistechnology.
onramp,asimpleWebSocketserver,isusedasthesignalingchanneltoestablishinitialconnections.
API//InitializingvarrootNode=P.create();//createtherootnode//ConnectionmanagementvarwebSocketNode=rootNode.connect(address);//connecttoanonrampWebSocketservervarwebRtcNode=webSocketNode.connect(address);//connecttoapeerusinganonrampconnectionvarwebRtcNode=webRtcNode.connect(address);//connecttoapeerusinganexistingpeerconnectionanyNode.close();//closetheconnectionanyNode.isOpen();//returntrueiftheconnectionisopenvarnodeArray=anyNode.getPeers();//returnsanarrayofallpeerconnections//FirewallingconnectionsvarprotectedNode=P.create({firewall:function(offerData){//OnlyacceptRTCconnectionofferswhichsend'secret'astheofferdata//thisfirewallrulewillapplytoanychildnodesaswellreturnofferData==='secret';}});//SendofferDatawithaconnectionrequestanyNode.connect({address:address,offerData:'secret'});//SendingandreceivingmessageswebRtcNode.send(message);//sendamessagetoapeer;canbejson,string,orarraybufferwebRtcNode.on('message',function(message){});//listensformessagesfromapeerwebRtcNode.on('arraybuffer',function(arrayBuffer){});//listensforarraybuffersfromapeer//EventsanyNode.on('connection',function(peerNode){});//emittedwhenaconnectionismadeviathispeeranyNode.on('open',function(){});//emittedwhenthisconnectionisopenandreadyanyNode.on('close',function(){});//emittedwhenthisconnectionisclosedanyNode.on('error',function(err){});//listensforerrorsforthisconnectionanyNode.removeListener(eventName,optionalCallback);//stopslisteningtoaneventDocumentationExamplewithwalkthroughAPICookbookContributeReleaseNotes0.3.3-FixedanissueinFFwhichcauseddisconnectionsshortingaftersuccessfulconnectionsduetoaGCmiscommunicationwhichseemstocleanupDataChannelswhicharestillinuse(therebyclosingthem).0.3.2-UpdatedthelibrarytoalignitwiththelatestWebRTCstandards--mainlychangesinconfigurationobjects.0.3.1-Added'firewall'optiontofirewallRTCrequests.0.3-MajorrefactorofinternalsandsimplificationofAPI,Firefoxsupport,andrespectableunittestcoverage.0.2-Publicrelease
评论