如何在java网站中构建实时应用程序

发布于 2024-09-25 18:14:44 字数 84 浏览 1 评论 0原文

我想使用java构建实时应用程序。 Web应用程序总是每10秒向服务器请求一次数据,并在网页上显示最新数据。

你知道如何解决这样的案件吗?

I want to build realtime application using java. The web application always request data to server every 10 seconds, and display the fresh data on the web page.

do you have idea how to solve case like this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听风吹 2024-10-02 18:14:44

Ajax 将使您的 JavaScript 代码轮询您的站点服务器以获取货币的最新值(例如每 10 秒一次)。这意味着如果有 100 人连接,您将在 10 秒内平均获得 100 次访问。

除非您使用 Flash 对象或 Java Applet 与服务器建立 TCP-IP 连接(可以在新值可用时推送新值),否则 Ajax 对您来说是更好/更简单的选择。 TCP 替代方案可提供更快的结果(与 Ajax 轮询相比,客户端可以实时看到新值),并且通常在性能方面更高效(仅推送有新值可用)。

如果您实现 Ajax 轮询系统,则必须添加服务器端滥用检测器:许多人都很好地理解 JavaScript,其中一些人可能会更改轮询频率以更快地获得更新的值...(就像每一秒)。取决于受众、访问您网站的人数等...
该检测器将确保给定客户端不会超过轮询频率(例如,每 12 秒一次以上,如果频率为 10,则有误差范围)

Ajax will make your JavaScript code poll your site server to get the last value of the currency, say every 10 seconds. Meaning if 100 persons are connected, you'll get on average 100 accesses in 10 seconds.

Unless you use a Flash object or a Java Applet to establish a TCP-IP connection with your server (that can push the new value when it is available), Ajax is a better/easier option for you. The TCP alternative provides faster results (clients see the new value more in real time than with Ajax polling), and, usually, more efficient in terms of performance (Only push when a new value is available).

If you implement an Ajax polling system, you'll have to add server side an abuse detector: many people understand well JavaScript and some of them may change the polling frequency to have newer values faster... (like every second). Depends on the audience, the number of people accessing your site etc...
That detector would ensure that a given client does not exceed the polling frequency (e.g. more than once every 12 seconds, if the frequency is 10, with an error margin)

水溶 2024-10-02 18:14:44

使用ajax每10秒查询一次服务器。您可以通过在前端使用 jquery 轻松实现此目的。

您的后端不需要任何特殊的东西,但一个好主意是使用 Web 服务,它将返回一个 XML 或 JSON,由前端 jquery 解析以更新显示。

Use ajax to query the server every 10 seconds. You can easily achieve this by using jquery in the frontend.

Your backend don't need anything special but a good idea is use web service which will return an XML or JSON that will be parsed by the front end jquery to update the display.

白云悠悠 2024-10-02 18:14:44

如果您正在寻找 java 框架,那么 playframeworkwebfirmframework。 Spring + Angularjs 是另一种方法。他们都提供了 websocket 通信,这对于实时更新很有用。使用半开状态或我们自己的实现来处理所有内容将需要一些时间才能变得稳定,因为要处理心跳、及时打开和关闭。关闭、重新连接套接字等......可能有点困难。如果你用谷歌搜索,你可以找到很多关于它们的教程。

If you are looking for a java framework then playframework or webfirmframework can be used. Spring + Angularjs is another way to do. Thay all are providing websocket communication which can be useful for realtime update. Handling everything with an ajar or our own implementation will take a bit time to become stable, because to handle hearbeat, intime opening & closing, reconnecting socket etc.. might be a bit difficult. If you google you can get a lot of tutorials for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文