【问题标题】:Unable to connect to Openfire server from the script running browser side无法从运行浏览器端的脚本连接到 Openfire 服务器
【发布时间】:2018-07-22 06:58:32
【问题描述】:

起初,我使用以下脚本连接到 Openfire。

const {Client} = require('@xmpp/client')
const client = new Client()
client.start('xmpp://localhost:5222').catch(err => {
    console.error('start failed', err)
})
client.handle('authenticate', authenticate => {
  return authenticate('gabbar', 'gabbar@123')
})

但它向我显示了一个错误“未定义要求”。所以我搜索了互联网,发现 browserify 可以完成我的工作。所以我使用 HTML 页面的 index.js 制作了 bundle.js 文件,并将其包含在 HTML 页面中。

<head>
   <meta charset="UTF-8"/>
   <title>xmpp.js example</title>
   <script src="bundle.js"></script>
  <!-- <script src="index.js"></script>-->
 </head>

但是我得到了错误

找不到兼容的连接方法

有没有人可以告诉任何其他的方式来做到这一点。我也尝试了与xmpp.js client 包的示例目录中给出的相同,但这给了我错误,例如 XMPP 不是函数。以下是我查看示例文件后编写的代码。

index.js

const {xmpp, xml} =
    typeof require === 'undefined' ? window.xmpp : require('@xmpp/client') // For you; require('@xmpp/client')
const {client} = xmpp()
client.start('xmpp://localhost:5222').catch(err => {
    console.error('start failed', err)
})
client.handle('authenticate', authenticate => {
  return authenticate('gabbar', 'gabbar@123')
})

sample.html

<head>
    <meta charset="UTF-8"/>
    <title>xmpp.js example</title>
    <script src="node_modules/xmpp.js/dist/xmpp.min.js"></script>
    <script src="index.js"></script>    
  </head>

这是我尝试从浏览器端连接到 openfire 的两种方式,但它们都不适合我。拜托,谁能告诉我我做错了什么或任何其他可能的更好的方法?

【问题讨论】:

    标签: javascript node.js xmpp openfire node-xmpp


    【解决方案1】:

    浏览器不支持xmpp://。浏览器仅支持 ws:// (websockets)。如果服务器支持 websockets,你可以这样做:

    client.start('ws://domain:port)client.start('ws://domain:port/xmpp-websockets)

    另一种选择是不在浏览器中使用 Node。这可以通过在没有浏览器的情况下自行运行 node 或在 Electron 的后台进程中运行该代码来完成(与单独运行 node 相同,但您可以与渲染器进程通信以与 UI 交互)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-19
      • 2015-01-04
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      相关资源
      最近更新 更多