LoginSignup
2
2

More than 5 years have passed since last update.

PeerJSのBroker IDを短くする

Posted at

PeerJSのデフォルトBroker IDは長くて,手打ちが大変.

IDを指定せずに使うと英数字混在で16文字のIDが返ってくる.

var peer = new Peer([id], [options]);

peer.on('open', function(id) {
    log.i(id); // =>計16文字のBroker ID
});

毎回手打ちするのも大変なので,短い文字列で済むようにしたい.

しかし,リロードなどで同一のBroker IDを連続して取得しようとすると一定時間?IDを受け付けない様子.

そこで,短い文字列で十分ランダムな文字列を生成して,Broker IDとして指定できるようにした.

短縮版Broker IDのサンプル

// ランダムな文字列を生成する.
// @return  文字列 英字3文字+秒(1−2桁)の組み合わせ
function getRandomString() {
  // ランダムな文字列を生成する元の文字列
  var BaseString ='abcdefghijklmnopqrstuvwxyz';

  var  randomString = "";

  for(var i =0; i<3; i++) {
    randomString += BaseString[Math.floor(Math.random() * BaseString.length)];
  }

  randomString += new Date().getSeconds();
  return randomString;
}

var peer = new Peer( getRandomString(), {key: apikey, debug : DEBUG});

peer.on('open', function(id) {
    log.i(id); // =>英字3文字+秒(1−2桁)の短縮版Broker ID
});

これで十分覚えられる範囲になりました.
良いWebRTCライフを!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2