连接 gun db 中的对等体

connect peers in gun db

我需要了解如何在 gun db 中连接对等点。我在 heroku 上部署了一个 socket.io 服务器,但我不知道它是否适用于 gun。任何有 gun db 经验的人都可以帮助我吗?我已经阅读了文档,但不清楚对等点是如何连接的,文档中也没有明确的代码示例。

我已经测试了聊天示例,但它不能在我的本地主机服务器上运行,我将无法将它部署到我的共享主机上,因为套接字是不允许的。同样在本地主机上,消息不会在两个不同的浏览器之间传递 windows。

更新:

这是添加了建议 lib/webrtc 的代码。还是不行。

index.html

<!DOCTYPE html>
<html>

<head>
  <title>Converse</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
  <link rel="stylesheet" type="text/css" href="../style.css">
  <link href='https://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'>
  <style>
    .chat__heading {
      position: fixed;
      text-align: center;
      z-index: 1;
      width: 100%;
      margin-top: 0;
      margin-bottom: 0;
    }

    .chat__form-container {
      display: flex;
      justify-content: center;
      width: 100%;
      padding: 10px 20px;
      position: fixed;
      z-index: 1;
      bottom: 0;
    }

    .chat__form {
      display: flex;
      justify-content: center;
      height: 50px;
      background-color: white;
      border: 2px solid white;
      max-width: 900px;
      width: 100%;
      border-radius: 5px;
    }

    .chat__name-input {
      flex: 1;
      padding: 10px;
    }

    .chat__message-input {
      flex: 5;
      padding: 10px;
    }

    .chat__submit {
      padding: 10px;
      color: white;
      border-radius: 5px;
    }

    .chat__submit:hover::after {
      background-color: rgba(0,0,0,0.2);
    }

    .chat__submit:focus::after {
      background-color: rgba(0,0,0,0.2);
    }

    .chat__submit::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      border-radius: 5px;
      transition: background-color 0.3s;
      background-color: rgba(0,0,0,0);
    }

    .chat__message-list {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;
      overflow-y: auto;
      padding: 60px 20px;
      width: 100%;
      background-color: rgba(0, 0, 0, 0.2);
      min-height: 100vh;
    }

    .chat__message {
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 10px;
      padding: 10px;
      border-radius: 5px;
      width: 100%;
      position: relative;
      max-width: 900px;
    }

    .chat__name {
      margin-right: 20px;
    }

    .chat__when {
      position: absolute;
      top: 0;
      right: 2em;
      padding: 10px;
      background: rgba(100%, 100%, 100%, 0.9);
      opacity: 0;
      border-radius: 5px;
    }

    .chat__message:hover .chat__when {
      opacity: 1;
      right: 0em;
    }

    @media (max-width: 567px) {
      .chat__heading {
        font-size: 30px;
      }
    }
  </style>
</head>

<body>
  <div class="chat hue2 page">
    <h2 id='title' class="chat__heading hue2 whitet">Have a Conversation...</h2>
    <ul class="chat__message-list">
      <li class="none"></li>
    </ul>

    <div class="chat__form-container hue2">
      <form class="chat__form">
        <label for="name-input" class="visually-hidden">Name</label>
        <input id="name-input" class="chat__name-input" placeholder="Name"></input>
        <label for="message-input" class="visually-hidden">Message</label>
        <input id="message-input" class="chat__message-input" placeholder="Write a message..."></input>
        <button class="chat__submit say hue2">say</button>
      </form>
    </div>

    <div class="model">
      <li class="chat__message white huet2 box">
        <b class="chat__name"></b>
        <p class="chat__message-text"></p>
        <span class="sort none">0</span>
        <div class="chat__when"></div>
      </li>
    </div>
  </div>

  <script src="../jquery.js"></script>
  <script src="../../gun.js"></script>
  <script src="../../nts.js"></script>
  <script src="../../lib/webrtc.js"></script>
  <script>
    var gun = Gun(document.location.host + ':443/gun');
    var chat = gun.get('converse/' + location.hash.slice(1));
    console.log(chat);
    console.log(gun);
    console.log(location.hash.slice(1));

    $(".chat__submit").on('click', submit);
    $(".chat_form").on('keydown', enter);
    function enter(e) {
      if (e.which !== 13) { return }
      submit(e);
    }
    function submit(e) {
      e.preventDefault();

      var msg = { when: Gun.time.is() };

      msg.who = $('.chat__name-input').val();
      if (!msg.who) {
        msg.who = 'user' + Gun.text.random(3);
        $('.chat__name-input').val(msg.who);
      }

      msg.what = $('.chat__message-input').val();
      if (!msg.what) { return }

      chat.set(msg);
      $('.chat__message-input').val('').focus();
    }

    chat.map().val(function (msg, id) {
      if (!msg) { return }
      var messageList = $('.chat__message-list');
      var last = sort(msg.when, messageList.children('li').last());

      var li = $("#msg-" + id)[0]; // grab if exists
      if (!li) {
        li = $('.model li').clone(true) // else create it
          .attr('id', 'msg-' + id)
          .insertAfter(last);
      }

      // bind the message data into the UI
      li = $(li);
      li.find('.chat__name').text(msg.who);
      li.find('.chat__message-text').text(msg.what);
      li.find('.sort').text(msg.when);

      var time = new Date(msg.when);
      li.find('.chat__when').text(time.toDateString() + ', ' + time.toLocaleTimeString());

      $('html, body').stop(true, true)
        .animate({ scrollTop: messageList.height() });
    });

    function sort(num, li) { return parseFloat(num) >= parseFloat($(li).find('.sort').text() || -Infinity) ? li : sort(num, li.prev()) }
  </script>
</body>

</html>


哇@mauro-stepanoski 的评论太好了(应该是答案)! @jihuuNI 当像 Todo-Dapp 教程中那样添加 lib/webrtc 适配器时,它会尝试自动将所有浏览器与所有其他浏览器连接 - 将来,AX 将自动切断不必要的连接。请注意,浏览器的 WebRTC 功能不是很可靠,因此您仍然希望拥有超级对等连接。