我正在为浏览器制作一个 Openfire 客户端。但我不知道如何将浏览器连接到本地主机上运行的 Openfire 服务器。 xmpp.js 和 Openfire 的文档都不充分且不清楚。网上也没有任何资料可以清楚地解释 xmpp.js 库的工作原理。
index.html
<head>
...
<script src="https://unpkg.com/@xmpp/[email protected]/dist/xmpp.min.js" crossorigin></script>
<script src="app.js" type="module" defer></script>
...
</head>
app.js
const { client, xml, jid } = window.XMPP;
const xmpp = client({
service: "ws://127.0.0.1:7070/ws/",
domain: "myDomain",
username: "myUsername",
password: "myPassword",
});
xmpp.on("error", (err) => {
console.error(err);
});
xmpp.on("offline", () => {
console.log("offline");
});
xmpp.on("status", (status) => {
console.log(status);
});
xmpp.on("stanza", async (stanza) => {
if (stanza.is("message")) {
await xmpp.send(xml("presence", { type: "unavailable" }));
await xmpp.stop();
}
});
xmpp.on("online", async (address) => {
// Makes itself available
await xmpp.send(xml("presence"));
// Sends a chat message to itself
const message = xml(
"message",
{ type: "chat", to: address },
xml("body", {}, "hello world"),
);
await xmpp.send(message);
});
xmpp.start().catch(console.error);
我已附上我在浏览器控制台上遇到的错误。任何帮助都会很棒,谢谢。
我尝试了各种服务参数的 URL,例如'xmpp://127.0.0.1:5222' 但从我从 Openfire 社区的一个答案中读到的内容来看,直接使用 xmpp 协议不是办法,我们必须使用 'ws' 协议。 (https://discourse.igniterealtime.org/t/how-to-connect-to-openfire-using-websocket/81860/2) (https://discourse.igniterealtime.org/t/help-xmpp-js-interoperate-with-openfire/89867/2)
找到解决方案
Web 绑定设置默认处于禁用状态,即使它列在管理面板(facepalm)的首页中。
转到服务器设置 -> Web 绑定并启用它。
如果您无法(像我一样),请更改端口并重试,它将启用。