创建聊天室应用程序时出现问题

发布于 2024-10-22 04:16:23 字数 437 浏览 3 评论 0原文

我正在尝试为某个网站创建一个类似聊天室的应用程序。为此,我有两个选择:

  1. 使用套接字编程并在服务器上打开套接字并将其连接到该聊天室中的所有客户端。为此客户端首先下载聊天室的小程序。

  2. 只是用Ajax以1秒的间隔向服务器连续发送请求,刷新页面的聊天内容区域。

我无法决定哪种策略更好。因此,如果有人告诉我哪个资源密集程度较低,并且还有其他更好的选择,请告诉我。

其次,我正在考虑使用存储在服务器上的会话文件来维护所有登录用户的会话。那么我应该如何访问存储在服务器上的文件,以便我可以拥有一些会话对象的成员变量,例如

sessionobject.chatroom="1"。 //请不要关注语法,而要关注其含义。

那么是否可以在服务器端访问服务器创建的文件来维护会话呢?如果是的话怎么办?

I am trying to create a chatroom like application for some website. For this I am having two options:

  1. To use socket programming and open a socket at server and connect this to all the clients who are in that chat room. for this client first download the applet of the chat room.

  2. just to send requests to server with Ajax continuously with 1 second interval and refreshing the chat content area of page.

I can't decide which stretagy will be better. So if anyone tell me which will be less resource intensive and if there is still other better option then please tell me.

Secondly I was thinking to use the session file stored on the server which maintain all logged in user's sessions. So how should i access that file stored at the server so that i can have some session object's member variable like

sessionobject.chatroom="1". //Please donot go to syntax but on its meaning.

So is it possible to access the file created by the server at the server for maintaining sessions? and if yes then how?

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

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

发布评论

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

评论(1

—━☆沉默づ 2024-10-29 04:16:23

对于客户端,我会看一下 Comet,这是用于向浏览器执行服务器端推送的技术术语。您可以查看多种方法来执行此功能,其中您提到了两种方法(长套接字和轮询)。这两种技术都可以使用 CometD 来执行,CometD 是 Dojo 基金会使用 Bayeux 规范构建的 JavaScript 库。

至于确定哪种方法更好,您需要查看您的基础设施。许多服务器受到处理线程数量的限制,并且每次只能处理一定数量的传入套接字。一旦达到限制,任何其他套接字都将排队(或根据服务器丢弃),直到释放套接字为止。 Tomcat6 以及其他较新的服务器确实支持使用 NIO API,它允许非阻塞客户端套接字处理,从而消除了对传入套接字连接的限制。如果您在客户端和您自己之间有任何 Web 服务器、防火墙、代理、负载均衡器等具有套接字限制,则您需要在最终解决方案中考虑到这一点。如果您的基础设施可以支持这种方法,那么它会非常有效,因为它可以为您的客户提供最快的响应时间并降低套接字设置和拆除的成本。如上所述的缺点是您的基础设施需要支持预期的最大用户数量(支持包括文件描述符等)。

使用轮询的替代方法:虽然会增加更多开销并且由于不始终处于连接而导致响应时间变慢,但其优点是允许您的后端可能使用更少的资源来支持相同数量的用户(更少的文件描述符等)。 .)。

至于你的第二个问题,我不太清楚你想问什么。如果您尝试在用户生成的请求之外访问该用户会话中的信息,则这是规范所不允许的,并且将被视为安全违规。如果您正在讨论在用户请求期间存储和访问用户会话中的信息,那么可以使用标准 HttpSession API。我建议您不要使用或尝试使用第一种方法,因为它不是一个好的设计。如果您需要维护所有用户线程都可以访问的用户数据,那么您将需要维护单个用户会话外部的数据(数据库、文件等)。

希望这有帮助。

For the client-side, I would take a look at Comet, which is the technology term for performing server-side push to a browser. There are a number of methods you can review to perform this functionality, two of them you mentioned (long socket and polling). Both of these techniques can be performed using CometD which is a JavaScript library built by the Dojo Foundation using the Bayeux specification.

As for determining which approach is better, you need to look at your infrastructure. A lot of servers are limited by the number of processing threads and can only handle a certain number of incoming sockets at any one time. Once you reach the limit, any further sockets will be queued (or dropped depending on server) until sockets have been released. Tomcat6, along with other newer servers do support use of the NIO APIs which allows for non-blocking client socket processing, which removes the restrictions on incoming socket connections. If you have any web server, firewall, proxy, load balancer, etc.. between the client and yourself that has a socket limit, you will need to take that into account in your final solution. This approach works great if your infrastructure can support it as it gives your clients the quickest response time and lowers the cost of socket setup and takedown. The downside as mentioned is that your infrastructure would need to support your maximum number of users expected (support includes file descriptors, etc..).

The alternative method of using polling: while adding more overhead and has slower response time due to not always being connected, has the benefit of allowing your backend to potentially use less resources to support the same number of users (less file descriptors, etc...).

As for your second question, I'm not really sure what you are asking. If you are trying to access information in a user's session outside of a request generated by that user, that is not something allowed by the specification and would be considered a security violation. If you are talking about storing and accessing information in a user's session during a request by that user, then that is possible using the standard HttpSession APIs. I would recommend you do not use or attempt to use the first approach as it is not a good design. If you need to maintain user data that needs to be accessible by all user threads, then you will want to maintain that data external to an individual user's session (database, file, etc..).

Hope this helps.

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