Java Spring MVC WebApp 中的推送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20901210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:08:57  来源:igfitidea点击:

Push Notification in Spring MVC WebApp

javaspringspring-mvcpush-notificationcometd

提问by COBOL

I am currently writing a forum web-application using Spring MVC. I'm just a beginner at Spring and have only been working with it for about 1 week now.

我目前正在使用 Spring MVC 编写论坛 Web 应用程序。我只是 Spring 的初学者,现在只使用它大约 1 周。

I need to implement push notifications. Here is the scenario: User A logs in and creates a post. User B comments on User A's post while User A is still logged in. User A receives a notification that some user has commented on his post, without his browser refreshing the page.

我需要实现推送通知。场景如下:用户 A 登录并创建帖子。用户 B 在用户 A 仍处于登录状态时对用户 A 的帖子发表评论。用户 A 收到通知,指出某个用户对其帖子发表了评论,而他的浏览器并未刷新页面。

I need help with sending the notification to User A that User B has commented on his post asynchronously. I have done some research, and found that there is a package called CometD that I can use, but I cannot find any simple tutorials for me to understand.

我需要帮助将通知发送给用户 A,即用户 B 异步评论了他的帖子。我做了一些研究,发现有一个名为 CometD 的包可以使用,但我找不到任何简单的教程让我理解。

Can anyone suggest any other packages/way to solve my problem? Or if you have any simple CometD tutorials, that would be great as well.

任何人都可以建议任何其他软件包/方法来解决我的问题吗?或者,如果您有任何简单的 CometD 教程,那也很棒。

回答by Angular University

Have a look at the spring MVC chat sample appfor spring MVC 3.2, there is also a spring blog postabout it.

查看spring MVC 3.2的spring MVC 聊天示例应用程序,还有一篇关于它的 spring博客文章

回答by sbordet

I am the CometD project lead.

我是 CometD 项目的负责人。

CometD ships with a demo war file that has a built-in chat application that you can use as a basis to understand how CometD works, see https://docs.cometd.org/current/reference/#_installation.

CometD 附带一个演示 war 文件,其中包含一个内置聊天应用程序,您可以将其用作了解 CometD 工作原理的基础,请参阅https://docs.cometd.org/current/reference/#_installation

The CometD full reference documentation is here: https://docs.cometd.org.

CometD 的完整参考文档在这里:https: //docs.cometd.org

From experience, I don't recommend using Servlet 3.0 async features to implement a chat, because that will rule out usage of WebSocket.

根据经验,我不建议使用 Servlet 3.0 异步功能来实现聊天,因为这会排除 WebSocket 的使用。

It's better you rely on frameworks such as CometD that can start with WebSocket (as it is more efficient) and then fallback automatically and transparently to HTTP if WebSocket does not work.

最好依赖诸如 CometD 之类的框架,这些框架可以从 WebSocket 开始(因为它更有效),然后在 WebSocket 不起作用时自动且透明地回退到 HTTP。

On a similar note, I don't recommend a WebSocket only approach because WebSocket does not work in certain scenarios (for example, mobile networks often do not allow WebSocket traffic), and it's a too low-level protocol so you will have to build your own protocol on top of WebSocket (which is a lot of work and not easy to get right).

同样,我不推荐只使用 WebSocket 的方法,因为 WebSocket 在某些情况下不起作用(例如,移动网络通常不允许 WebSocket 流量),而且它是一个太低级的协议,因此您必须构建在 WebSocket 之上的你自己的协议(这是很多工作,不容易做到正确)。

CometD provides you with an easy to extend protocol and automatic transport fallback, and has a ton of features (e.g. authorization & authentication and clustering just to mention two).

CometD 为您提供了易于扩展的协议和自动传输回退,并具有大量功能(例如授权和身份验证和集群,仅举两个)。