jandres
(Julian)
2024 年2 月 7 日 22:12
1
大家好
我正在尝试了解 Discourse 用于保持在线状态以接收新消息和事件的消息总线功能是如何工作的。我特别对它在最近的 Discourse 聊天插件中是如何使用的感兴趣。
根据 @sam 在 https://meta.discourse.org/t/how-discourse-stays-online-message-bus-faye-long-polling/3238/7?u=jandres 中描述的内容,我想将此功能与已有的功能集成到我的移动应用程序中。
通过一些逆向工程,我发现当我进入聊天室时,每次消息总线通过 “https://mysite.com/message-bus/client_id/poll ” 进行轮询时,请求头都会被复制,例如:
这在我的网站上运行正常,但当我尝试从外部网站或移动应用程序发出请求时,我会收到错误:
Access to XMLHttpRequest at 'https://forum-dev.srepe.com/message-bus/f6e2303ff7704f608ef2b821fe210e90/poll'
from origin 'http://localhost:8100' has been blocked by CORS policy:
The 'Access-Control-Allow-Origin' header contains multiple values
'https://forum-dev.srepe.com, *', but only one is allowed.
所有其他路由都很好,但当我尝试使用消息总线时就会发生这种情况。我正在使用 @sam 在 github 存储库 https://github.com/discourse/message_bus/tree/main/examples/chat 中留下的示例作为灵感来源。
如果您能给我一些指导或帮助,我将不胜感激
非常感谢
1 个赞
jandres
(Julian)
2024 年2 月 7 日 23:17
3
感谢您的快速回复。
那么,如果我理解正确的话,message_bus 会设置自己的“Headers”?有没有办法使用 Discourse 配置中已设置的“Headers”,至少是那些具有 CORS 行为的?
我想避免在使用 message_bus 时重复设置“headers”。这使我能够从论坛站点外部发出请求。
sam
(Sam Saffron)
2024 年2 月 7 日 23:19
4
你离通用配置相去甚远,请仔细阅读该文件,它应该包含所有答案。
jandres
(Julian)
2024 年2 月 7 日 23:37
5
Sam Saffron:
你的配置相当不寻常
你说得对。那么,如果我从站点外部(例如在 PWA 或移动混合应用中)使用 message_bus,你有什么建议?
我想将 message_bus 用作外部客户端,例如使用你们已经制作的 JavaScript 库。 https://github.com/discourse/message_bus/tree/main?tab=readme-ov-file#javascript-client
jandres
(Julian)
2024 年2 月 13 日 02:05
6
我想知道这位用户是如何做到的。
Yeah, the whole backend is powered by message_bus. The android/ios client long-polls. Not sure if this is the best use of message_bus, but It works w/o the added complexity of web sockets. The major win is that it fits in our existing rails stack. The only disadvantage i’ve come across so far is that I can’t find an elegant way to support the typing indicator.
http://www.eggie5.com/58-real-time-messenging-w-ruby-ios-android
他在这里 发布了一个 YouTube 视频,展示了他在多个设备上的进展。
应该有一种方法可以在 discourse 站点之外使用 message_bus gem。
如果您能再指导我一点,或者给我一些想法,我将不胜感激。
sam
(Sam Saffron)
2024 年2 月 13 日 02:35
7
恐怕这对我来说太定制化了,无法在此提供建议,我非常赞赏更多人使用消息总线,但您必须在此处调试。