【发布时间】: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