在浏览器中使用 xmpp.js 连接到 XMPP 服务器

Connect to XMPP server using xmpp.js in the browser

我正在尝试 运行 在浏览器中使用本地 xmpp 服务器进行测试。

import { client } from '@xmpp/client';

const xmpp = client({
  service: 'xmpp://localhost:5222/',
  username: 'user',
  password: 'pass',
});

xmpp.start().catch(err => {
  console.error('start failed', err);
});

但我收到以下错误:

No compatible connection method found.

据我了解,浏览器需要 websocket 连接而不是 xmpp 连接。

xmpp.js 文档说它也支持 websockets,但我不确定我必须更改什么才能成功连接。我必须向我的 XMPP 服务器添加 WS 支持吗?

很可能您必须更改 service URI - 协议和端口部分。根据 xmpp.js client documentation 你应该使用:

service: 'ws://localhost:5280/xmpp-websocket',

确切路径可能取决于 xmpp 服务器(即可能不需要 xmpp-websocket)- 请检查您服务器的文档。