将聊天服务器实现为 WebService
我有一个学校项目,我必须在其中实现一个聊天应用程序,其服务器将是一个 java web 服务。
问题是,我一直认为 Web 服务是调用远程功能的一种方式,而且我不知道如何在 Web 服务上保持“会话”活动,也不知道如何跟踪当前处于活动状态的所有人员。聊天、房间等
I have a school project in which I have to implement a chat application, whose server will be a java web service.
The problem is that I've always thought of a web service as a way of calling remote functions, and I have no idea how to keep a "session" active on the web service, nor how to keep track of all the people currently in chat, rooms etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,聊天服务器应该在初始连接后了解其客户端,并将每个客户端消息发送给所有客户端。 这肯定需要某种会话维护。 我认为正确的方法如下:
我不太确定消息分发应该如何工作,因为 Web 服务本质上是拉式服务而不是推送式服务。 也许客户端应该公开自己的 Web 服务以供服务器调用。
希望这有帮助,
尤瓦尔=8-)
To the best of my knowledge, a chat server is supposed to know its clients after an initial connection, and send every client message to all clients. This definitely calls for some sort of session maintenance. I think the right way to do this is as follows:
I'm not really sure how the message distribution should work, as web services are essentially a pull-service and not push. Perhaps the client should expose its own web service for the server to call.
Hope this helps,
Yuval =8-)
您可以考虑实施 COMET 解决方案。 这将有效地为您提供推送通信,从而消除延迟,这对于聊天应用程序来说是一个非常好的功能。
如果您想获得金牌,请考虑实施更高级的功能:
You could consider implementing a COMET solution. This will effectively give you push communication, thus eliminating latency, a VERY nice feature for a chat application.
If you want to go for the gold, consider implementing more advanced features:
我不了解 Java,所以这个答案与语言无关。
在我看来,无需在服务器上运行进程即可实现此目的的最简单方法是将所有数据存储在数据库中。
以下是需要完成的基本事项的简短列表:
A. 需要最后一次联系的时间戳字段
如果您仍然需要一些帮助,这里有一个 AJAX/ASP.Net 聊天应用程序,它应该(我没有查看其源代码)以大致相同的方式工作。
I don't know Java so this answer will be language agnostic.
In my opinion the simplest way to do this without running a process on the server would be to store all your data in a database.
Here is a short list of the basic things that will need to be done:
A. needs a time stamp field of the last contact
If you still need some help here is an AJAX/ASP.Net chat app that should (I didn't look at its source) work much the same way.
我编写了一个聊天引擎,它在后台有一个服务,所有内容都存储在数据库中,一个输入表单框架和一个接收 html 流的输出框架。
如果您想跳过服务部分并仅通过 Web 服务实现,则需要实现至少两个操作:Post 用于输入,以及 GetLatestChanges 用于接收聊天的输出,该输出使用一些 Javascript 魔法转换为 HTML。
当然,您需要跟踪房间、用户、消息、哪个用户收到哪些文本等,如 Unknwntech 所描绘的那样。
I wrote a chat engine which had a service in the background and everything stored in a database, an input form frame and an output frame which received the html stream.
If you want to skip the service part and only implement via a web service, you need to implement at least two operations: Post for inputs, and GetLatestChanges to receive the chat's output, which translates into HTML using some Javascript magic.
Of course you need to keep track of rooms, users, messages, which user receives which texts etc, as sketched by Unknwntech.