Javascript 无法从运行浏览器端的脚本连接到Openfire服务器
起初,我使用以下脚本连接到OpenfireJavascript 无法从运行浏览器端的脚本连接到Openfire服务器,javascript,node.js,xmpp,openfire,node-xmpp,Javascript,Node.js,Xmpp,Openfire,Node Xmpp,起初,我使用以下脚本连接到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 a
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>
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>
xmpp.js示例
这是我尝试从浏览器端连接到openfire的两种方式,但都不适用于我。请问,有谁能告诉我我做错了什么或者其他更好的方法吗?
xmpp://
在浏览器中不受支持。浏览器中仅支持ws://
(websockets)。如果服务器支持WebSocket,您可以执行以下操作:
client.start('ws://domain:port)
或client.start('ws://domain:port/xmpp-websockets)
另一个选项是使用不在浏览器中的节点。这可以通过在没有浏览器的情况下单独运行节点或在Electron的后台进程中运行代码来实现(与单独运行节点相同,但您可以与渲染器进程通信以与UI交互)
<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>