How to use the network/rtc.RTCPeerConn function in network

To help you get started, we’ve selected a few network examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async authenticatePeer(peer: SimplePeer.Instance, peerId?: string) {
    const peerPublicKey = peerId ? sodium.from_hex(peerId) : undefined
    const userPublicKey = await mutualHandshake(peer, localUser().id, peerPublicKey)

    if (!userPublicKey) {
      const addr = `${(peer as any).remoteAddress}:${(peer as any).remotePort}`
      peer.destroy()
      throw new NetworkError(
        NetworkErrorCode.PeerAuthenticationFailure,
        `Failed to authenticate with peer [${addr}]`
      )
    }

    const userId = sodium.to_hex(userPublicKey)
    const netId = new NetUniqueId(userPublicKey)
    const conn = new RTCPeerConn(netId, peer)

    console.debug(`Authenticated peer ${userId}`, conn)
    this.emit('connection', conn)
  }
}
github samuelmaddock / metastream / app / platform / steam / peer-coordinator.ts View on Github external
private createPeer(steamId: Steamworks.SteamID): RTCPeerConn {
    const peer = new SimplePeer({
      initiator: this.isLobbyOwner,
      trickle: false,
      config: {
        iceServers
      }
    });

    const netId = new SteamUniqueId(steamId);
    const userId = steamId.getRawSteamID();

    const conn = new RTCPeerConn(netId, peer);
    this.connecting[userId] = conn;

    conn.once('connect', () => {
      conn.removeAllListeners();
      this.connecting[userId] = undefined;
      this.emit('connection', conn);
    });

    conn.once('close', () => {
      this.connecting[userId] = undefined;
    });

    return conn;
  }