【发布时间】:2020-12-14 08:48:46
【问题描述】:
我的公司选择“Mercure”(https://mercure.rocks/docs/getting-started) 来管理服务器发送的事件。 我们在服务器上安装“Mercure HUB”,现在,在 C# .NET 5.0 中,我必须实现服务器端(发布者,我已经实现)和客户端(订阅者)。 订阅者必须使用 WPF 完成
从“入门”页面我可以看到一个需要转换为 C# 的 Javascript 示例 我不知道如何在 C# 中管理“EventSource” 有什么想法吗?
// The subscriber subscribes to updates for the https://example.com/users/dunglas topic
// and to any topic matching https://example.com/books/{id}
const url = new URL('https://localhost/.well-known/mercure');
url.searchParams.append('topic', 'https://example.com/books/{id}');
url.searchParams.append('topic', 'https://example.com/users/dunglas');
// The URL class is a convenient way to generate URLs such as https://localhost/.well-known/mercure?topic=https://example.com/books/{id}&topic=https://example.com/users/dunglas
const eventSource = new EventSource(url);
// The callback will be called every time an update is published
eventSource.onmessage = e => console.log(e); // do something with the payload
【问题讨论】:
-
您的意思是您希望 C# 客户端而不是浏览器客户端来监听事件?还是因为您使用的是 Blazor?要不然是啥?您的解释缺乏上下文,并且不清楚您自己为此付出了哪些努力(如果有的话)。例如makolyte.com/… 看起来很有趣。你搜索过什么吗?
-
谢谢,我在原帖中添加了一些信息。我做了一些研究并尝试制作一些 GetAsync 来订阅一些事件,现在我尝试了你的建议,但没有任何效果。当我进行此异步调用时,似乎 WPF 页面卡在调用循环中,我无能为力
-
是的,这就是 WPF ......对我们有点帮助。但是您需要向我们展示您尝试过的代码,以便我们了解问题可能出在哪里。例如,您可能需要将监听事件的工作交给后台工作任务。
-
我的错,我管理了一个新线程,现在您的建议有效!非常感谢
-
没问题。如果您解决了它,请在下面添加您的代码解决方案,并附上简短的解释,以造福他人(如果它吸引了赞成票,也为了您的利益) - 谢谢。当问题也附有答案时,这个网站会更有用!
标签: javascript c# wpf .net-5 eventsource