node/src/p2p.js
// sendMessage transmits a string to a peer
sendMessage transmits a string to a peer
sendMessage transmits a string to a peer
function sendMessage(p2pId, port, otherPeer, otherPeerPort, message) {
    async.parallel([
      (cb) => PeerId.createFromJSON(require(p2pId), cb),
      (cb) => PeerId.createFromJSON(require(otherPeer), cb)
    ], (err, ids) => {
      if (err) { throw err }
      const dialerId = ids[0]
      const dialerPeerInfo = new PeerInfo(dialerId)
      dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
      const dialerNode = new Node({
	peerInfo: dialerPeerInfo
      })
      const listenerPeerInfo = new PeerInfo(ids[1])
      const listenerId = ids[1]
      const listenerMultiaddr = '/ip4/127.0.0.1/tcp/'+otherPeerPort+'/ipfs/' +
	  listenerId.toB58String()
      listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
      dialerNode.start((err) => {
	if (err) { throw err }
	console.log('Dialer ready, listening on:')
	dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
	      '/ipfs/' + dialerId.toB58String()))
	console.log('Dialing to peer:', listenerMultiaddr.toString())
	dialerNode.dialProtocol(listenerPeerInfo, '/message', (err, conn) => {
	  if (err) { throw err }
	  pull(
	    pull.values([message]),
	    conn,
	    pull.collect((err, data) => {
	      if (err) { throw err }
	      console.log('received:', data.toString())
	    })
	  )
	})
      })
    })
}
function sendMessage ( p2pId , port , otherPeer , otherPeerPort , message ) { async . parallel ( [ ( cb ) => PeerId . createFromJSON ( require ( p2pId ) , cb ) , ( cb ) => PeerId . createFromJSON ( require ( otherPeer ) , cb ) ] , ( err , ids ) => { if ( err ) { throw err } const dialerId = ids [ 0 ] const dialerPeerInfo = new PeerInfo ( dialerId ) dialerPeerInfo . multiaddrs . add ( '/ip4/0.0.0.0/tcp/0' ) const dialerNode = new Node ( { peerInfo : dialerPeerInfo } ) const listenerPeerInfo = new PeerInfo ( ids [ 1 ] ) const listenerId = ids [ 1 ] const listenerMultiaddr = '/ip4/127.0.0.1/tcp/' + otherPeerPort + '/ipfs/' + listenerId . toB58String ( ) listenerPeerInfo . multiaddrs . add ( listenerMultiaddr ) dialerNode . start ( ( err ) => { if ( err ) { throw err } console . log ( 'Dialer ready, listening on:' ) dialerPeerInfo . multiaddrs . forEach ( ( ma ) => console . log ( ma . toString ( ) + '/ipfs/' + dialerId . toB58String ( ) ) ) console . log ( 'Dialing to peer:' , listenerMultiaddr . toString ( ) ) dialerNode . dialProtocol ( listenerPeerInfo , '/message' , ( err , conn ) => { if ( err ) { throw err } pull ( pull . values ( [ message ] ) , conn , pull . collect ( ( err , data ) => { if ( err ) { throw err } console . log ( 'received:' , data . toString ( ) ) } ) ) } ) } ) } ) }
sendMessage transmits a string to a peer
sendMessage transmits a string to a peer
// Dialer // Peer to Dial