【问题标题】:Error during WebSocket handshake: Unexpected response code: 200 - Next.js, Gun.js, ZEIT NowWebSocket 握手期间出错:意外响应代码:200 - Next.js、Gun.js、ZEIT Now
【发布时间】:2020-01-15 13:02:32
【问题描述】:

我通过 ZEIT Now deployed 一个 Gun.js + Next.js 应用程序(来源 here),我从 Chrome 和 Safari 收到以下错误:

WebSocket connection to 'wss://maptivist.randymorantes.now.sh/' failed: Error during WebSocket handshake: Unexpected response code: 200

我从 Firefox 获得:

Firefox can’t establish a connection to the server at wss://maptivist.randymorantes.now.sh/.

此外,我的 Gun 同伴似乎没有相互交流,可能是因为这个错误。我猜这可能是a problem with my server configuration,但我不知道如何将各种解决方案应用于我的案例(example)。相关代码似乎是:

// in next.config.js

const nextConfig = {
  target: 'serverless',
  webpack: customWebpackConfig,
  workboxOpts: {
    swDest: 'static/service-worker.js',
    runtimeCaching: [
      {
        urlPattern: /^https?.*/,
        handler: 'NetworkFirst',
        options: {
          cacheName: 'https-calls',
          networkTimeoutSeconds: 15,
          expiration: {
            maxEntries: 150,
            maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
          },
          cacheableResponse: {
            statuses: [0, 200],
          }
        }
      }
    ]
  }
}

问题也可能是我如何使用 Gun(精简版):

import Gun from 'gun/gun'
import 'gun/sea'

const gun = Gun('https://maptivist.randymorantes.now.sh')
const user = gun.user()

user.create(valueAlias, valuePassword, response => {
  if (!response.err) {
    gun.get('users').set(user)
  }
})

// Only one user is found, the current user, despite there being
// two different users signed in on different devices.
gun.get('users').map(user => {
  console.log('user: ', user)
})

我的理解是,在创建用户时,我的两个对等点应该通过位于我的 URL 的第三个对等点自动同步这些更改。然而,Gun + Next example 使用一个 URL (const gun = Gun('https://gunjs.herokuapp.com/gun')),Gun todos example 使用两个 (var gun = Gun(['http://localhost:8765/gun', 'https://gunjs.herokuapp.com/gun']))。因此,我不清楚,对于我的用例,我是否还需要传递与我的本地计算机对应的 URL。

我尝试过的 URL 组合(在重新部署和刷新之后)(还不清楚是否需要将“/gun”附加到 URL 或约定):

const gun = Gun('https://maptivist.randymorantes.now.sh')
const gun = Gun('https://maptivist.randymorantes.now.sh/gun')
const gun = Gun(['http://localhost:3000/', 'https://maptivist.randymorantes.now.sh'])
const gun = Gun(['http://localhost:3000/', 'https://maptivist.randymorantes.now.sh/gun'])
const gun = Gun(['http://localhost:3000/gun', 'https://maptivist.randymorantes.now.sh/gun'])

我被难住了。请帮忙! :-)

【问题讨论】:

    标签: next.js gun vercel


    【解决方案1】:

    @randy-morantes ZEIT 支持 websockets 吗?如果是这样,这应该可以工作。

    我猜你可能需要/gun

    GUN 甚至在 ZEIT 服务器上运行吗?它看起来不像。您可以粘贴使用 GUN 的服务器 ZEIT 代码吗?

    浏览器不需要localhost在本地运行GUN,它会直接在浏览器中运行GUN。但是,开发人员/用户也可以在本地(与浏览器分开)在 NodeJS 中运行 GUN,这就是 localhost 所引用的内容,除非他们有一个本地运行的 GUN NodeJS 对等体处于活动状态(这不太可能),否则将无法工作。

    您可能可以在友好的https://gitter.im/amark/gun 社区聊天中获得更多即时帮助,但请记住回到这里并用您所学的内容更新您的帖子和答案,以便其他人也可以从中受益。 :)

    【讨论】:

    • 我没有服务器代码,这是问题所在。 const gun = Gun('https://gunjs.herokuapp.com/gun') 让它工作,也消除了我的一个误解。我现在知道 Gun 客户端/对等点必须在某个时候连接到至少一个运行 Gun 的服务器/超级对等点以持久保存数据(1)(2) 并且(我认为)促进 WebRTC 连接(3)(关于此的文档可能是更清晰)。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多