简体   繁体   中英

PeerJs peer.connections deprecated

I am trying to get all peers that are currently connected to me, but it says connections is deprecated.

const peer = new Peer('my_id');
const connections = peer.connections;

Documentation says: We recommend keeping track of connections yourself rather than relying on this hash.

Let's say I keep track of all peers id in array. How should I handle the ids so that I get the same result as with peer.connections ?

Example: peer.connections returns: peer.connections 控制台.log

What method do I need to call to get the same results from peer id in my array?

I think you should create a listener when a connection is established. You may achieve this by implementing a wrapper that will unify client usage of peer js

class MyPeer {
  constructor() {
    this.instance = new Peer()
  }

  async connect() {
    return new Promise(resolve => {
      this.connection = peer.connect(new Date().getTime()) // create a unique id for each connection

      this.connection.on('open', () => resolve(this.connection))
    })
  }
}

Assuming you are taking this (very naïve, without proper error handling) implementation, you may keep track of connections like this

const peer = new MyPeer()
   
const connection = await peer.connect()
myConnectionsArray.push(connection)

It worth mentioning that you may remove connections from array when a connection is closed and probably wrap everything in a class (say ConnectionsManagaer or something similar). Besides that, it should satisfy your request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM