castonetwork / minimvp_channel

P2P webRTC Proof of Concept - Channel Web Application
0 stars 0 forks source link

peer status update #5

Closed acidsound closed 6 years ago

acidsound commented 6 years ago

peer:discovery 이외에 peer:connect와 peer:disconnect를 관리할 수 있어야함.

acidsound commented 6 years ago

최소형태로 websocket star를 사용하는 node가 peer:connect/disconnect event를 발생하도록 하는 샘플을 작성해본다.

acidsound commented 6 years ago

작게 작성하여 테스트 해 본 결과 muxer가 있어야 peer:connect/disconnect event가 발생.

const libp2p = require("libp2p");
const PeerInfo = require('peer-info');
const multiaddr = require('multiaddr');
const WSStar = require("libp2p-websocket-star");
const Mplex = require("libp2p-mplex");
//const SECIO = require("libp2p-secio");
class Node extends libp2p {
  constructor(_options) {
    const wsStar = new WSStar({id: _options.peerInfo.id});
    const defaults = {
      modules: {
        transport: [wsStar],
        streamMuxer: [Mplex],
 //       connEncryption: [SECIO],
        peerDiscovery: [wsStar.discovery]
      },
      config: {
        peerDiscovery: {
          websocketStar: {
            enabled: true
          }
        }
      }
    };
    super({...defaults, ..._options});
  }
}
const createNode = async () => new Promise((resolve, reject)=> {
  PeerInfo.create((err, peerInfo) => {
    if (err) reject(err);
    peerInfo.multiaddrs.add(multiaddr("/ip4/64.137.231.84/tcp/9090/ws/p2p-websocket-star/"));
    peerInfo.multiaddrs.add(multiaddr("/ip4/59.10.206.150/tcp/9999/ws/p2p-websocket-star/"));
    const node = new Node({peerInfo});
    resolve(node);
  });
});

const logTime = d => ~~((Date.now()-d)/1000);
(async ()=> {
  let d = Date.now();
  const nodes = [await createNode(), await createNode()];
  nodes.some(node => console.log(node.peerInfo.id.toB58String()));
  console.log(logTime(d), "nodes start");
  nodes.some(node => node.start());
  nodes[0].on("peer:discovery", peerInfo => {
    if (peerInfo.id.toB58String()=== nodes[1].peerInfo.id.toB58String()) {
      console.log(logTime(d), "node1 discovered");
      nodes[0].dial(peerInfo, (err, conn)=> {
        console.log(logTime(d), "dial callback");
      });
    }
  });
  nodes[0].on("peer:connect", peerInfo => {
    console.log("peer connect success");
  });
  nodes[0].on("peer:disconnect", peerInfo => {
    console.log("peer disconnecte4d");
  });
  setTimeout(()=> {
    nodes[1].stop();
  }, 25000);
})();
acidsound commented 6 years ago

streamer, channels, controller 모두 muxer를 포함하도록 수정해야함.

acidsound commented 6 years ago

e862f4108c5ca23f0d43c933726c51821656907d streamMuxer 추가