问题描述
为了测试与我们的Openfire XMPP服务器的客户端连接性,最初测试了以下测试脚本(nodejs),该脚本成功运行。
async function connectX() {
try {
const keyPair = await XmppUser.generateKeys()
XmppUser.connect(username,password,keyPairs).then(
async user => {
user.getFriends().then(friends => {
console.log("List of Friends: ");
console.log(friends);
user.getChatHistory().then(history => {
console.log("Chat History: ")
console.log(history);
user.closeConnection();
}).catch(e => console.log(e));
}).catch(e => console.log(e));
}
).catch(e => console.log(e));
} catch(e) {
console.log(e)
}
}
connectX()
输出:
List of Friends:
[ '[email protected]','[email protected]' ]
Chat History:
{ messages:
[ { date: 2020-08-11T12:18:34.314Z,from: '[email protected]',to: '[email protected]',message: 'Hello! Received.',id: 'D255Q-132' }
] }
lastId: '1',firstId: '1',count: '1' }
但是,当我使用相同的ChatService类在离子角度应用程序中运行脚本时,会导致出现以下CORS错误。
Access to fetch at 'https://chat.company.io/.well-kNown/host-Meta' from origin 'http://localhost:8100' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
有关信息, https://chat.company.io/.well-known/host-meta 存在且具有以下内容:
<?xml version='1.0' encoding='utf-8'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
<Link rel="urn:xmpp:alt-connections:httppoll"
href="https://chat.company.io:443" />
</XRD>
后端基础结构包括一个Nginx服务器,聊天服务器在其后方运行,并具有以下打开的端口:443、7070、5222、5223。
请帮助找出问题所在。
解决方法
可能发生的CORS问题, 在服务器上->服务器设置-> http绑定-> CORS部分中,在Openfire中启用对CORS的支持。