javascript - In Gun.js, how can I check if the peer (relay server) is connected? - Stack Overflow

I'm trying to create a multiplayer game with Gun.js.I made some tests with 2 browsers opened at s

I'm trying to create a multiplayer game with Gun.js.

I made some tests with 2 browsers opened at same time and noticed sometimes the changes in one window are not updating the other.

So, I would like to check if the problem is with the connection / peer.

I'm using this GUN relay URL:

How do I check if the relay is connected?

index.html

<body>
  <script src=".js"></script>
  <script src="gunjs-test.js"></script>

  <div>
    Relay URL:
    <input type="text" id="url" value="; style="width: 400px">
    <button onclick="initGun(document.getElementById('url').value)">Init gun</button>
  </div>

  <div>
    Root node name:
    <input type="text" id="rootNodeName" value="test">
  </div>

  <div>
    Node name:
    <input type="text" id="nodeName" value="paste">
  </div>
  
  <button onclick="enableRealtimeUpdates(document.getElementById('rootNodeName').value, document.getElementById('nodeName').value)">Enable realtime updates</button>

</body>

gunjs-test.js

let gun = null;

function initGun(url) {
  gun = Gun([url]);
}

function enableRealtimeUpdates(rootNodeName, nodeName) {
  gun.get(rootNodeName).get(nodeName).on((data, id) => {
    console.log("id: " + id);
    console.log("data: " + data);
  });
}

function isConnected() {
  // ???
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745586661a4634581.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信