# Message-bus 在 Discourse 聊天中重复复制标头

**URL:** https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607
**Category:** Development
**Tags:** chat, rest-api
**Created:** [2024年二月7日 22:12 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607 "2024-02-07T22:12:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [2024年二月7日 22:12 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/1 "2024-02-07T22:12:20Z")

</div>

大家好

我正在尝试了解 Discourse 用于保持在线状态以接收新消息和事件的消息总线功能是如何工作的。我特别对它在最近的 Discourse 聊天插件中是如何使用的感兴趣。

根据 @sam 在 [https://meta.discourse.org/t/how-discourse-stays-online-message-bus-faye-long-polling/3238/7?u=jandres](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](https://mysite.com/message-bus/client_id/poll)_” 进行轮询时，请求头都会被复制，例如：

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/2/d/d2d22182a9baab3cdbe6ed705dbf2925a665e59b.png)

这在我的网站上运行正常，但当我尝试从外部网站或移动应用程序发出请求时，我会收到错误：

```plaintext
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](https://github.com/discourse/message_bus/tree/main/examples/chat) 中留下的示例作为灵感来源。

如果您能给我一些指导或帮助，我将不胜感激 🙏

非常感谢

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024年二月7日 22:49 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/2 "2024-02-07T22:49:52Z")

</div>

headers 已在此处设置：

> <https://github.com/discourse/discourse/blob/ac53e5a9622bd118fffa37684c5b0f894e97b943/config/initializers/004-message_bus.rb#L17-L86>

希望这能回答这个问题？

---

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [2024年二月7日 23:17 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/3 "2024-02-07T23:17:23Z")

</div>

感谢您的快速回复。

那么，如果我理解正确的话，message\_bus 会设置自己的“Headers”？有没有办法使用 Discourse 配置中已设置的“Headers”，至少是那些具有 CORS 行为的？

我想避免在使用 message\_bus 时重复设置“headers”。这使我能够从论坛站点外部发出请求。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024年二月7日 23:19 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/4 "2024-02-07T23:19:20Z")

</div>

你离通用配置相去甚远，请仔细阅读该文件，它应该包含所有答案。

---

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [2024年二月7日 23:37 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/5 "2024-02-07T23:37:22Z")

</div>

> [@sam](#):
>
> 你的配置相当不寻常

你说得对。那么，如果我从站点外部（例如在 PWA 或移动混合应用中）使用 message\_bus，你有什么建议？

我想将 message\_bus 用作外部客户端，例如使用你们已经制作的 JavaScript 库。 [https://github.com/discourse/message\_bus/tree/main?tab=readme-ov-file#javascript-client](https://github.com/discourse/message_bus/tree/main?tab=readme-ov-file#javascript-client)

---

<div class="post-metadata">

### Author: ![jandres](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jandres/32/130933_2.png) [@jandres](https://meta.discourse.org/u/jandres)
#### Post date: [2024年二月13日 02:05 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/6 "2024-02-13T02:05:38Z")

</div>

我想知道这位用户是如何做到的。

> [@How discourse stays online (Message Bus, Faye, Long Polling)](https://meta.discourse.org/t/how-discourse-stays-online-message-bus-faye-long-polling/3238/22?u=jandres):
>
> 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](http://www.eggie5.com/58-real-time-messenging-w-ruby-ios-android)

他在[这里](https://youtu.be/5uNg9HopoGs?si=AebJZGi01dyRHhwq)发布了一个 YouTube 视频，展示了他在多个设备上的进展。

应该有一种方法可以在 discourse 站点之外使用 message\_bus gem。

如果您能再指导我一点，或者给我一些想法，我将不胜感激。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2024年二月13日 02:35 UTC](https://meta.discourse.org/t/message-bus-is-duplicating-headers-in-discourse-chat/294607/7 "2024-02-13T02:35:17Z")

</div>

恐怕这对我来说太定制化了，无法在此提供建议，我非常赞赏更多人使用消息总线，但您必须在此处调试。
