将聊天服务器实现为 WebService

发布于 2024-07-21 07:03:10 字数 148 浏览 5 评论 0原文

我有一个学校项目,我必须在其中实现一个聊天应用程序,其服务器将是一个 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 技术交流群。

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

发布评论

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

评论(4

新一帅帅 2024-07-28 07:03:10

据我所知,聊天服务器应该在初始连接后了解其客户端,并将每个客户端消息发送给所有客户端。 这肯定需要某种会话维护。 我认为正确的方法如下:

  1. 客户端调用 Web 服务“握手”并提供一些最少的识别详细信息。
  2. 服务器返回包含唯一客户端标识符的确认。
  3. 客户端调用 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:

  1. Client calls web service 'handshake' and provides some minimal identification details.
  2. Server returns an acknowledgment that includes a unique client identifier.
  3. Client calls web service 'message' and sends a new message, together with its identifier.
  4. Server identifies client by the identifier, distributes message to all clients.

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-)

飘落散花 2024-07-28 07:03:10

您可以考虑实施 COMET 解决方案。 这将有效地为您提供推送通信,从而消除延迟,这对于聊天应用程序来说是一个非常好的功能。

如果您想获得金牌,请考虑实施更高级的功能:

  • 拼写检查
  • URL/电子邮件地址自动转换为链接
  • 单独的聊天室
  • 主持人功能(终止聊天、踢出用户)
  • 事件信息,例如“用户正在输入...”
  • 状态(有空、忙碌、离开...)
  • 头像
  • ...

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:

  • spell check
  • URLs/email addresses converted to links automatically
  • separate chat rooms
  • moderator functions (terminate chat, kick user)
  • event info like "User is typing..."
  • statuses (available, busy, away...)
  • avatars
  • ...
披肩女神 2024-07-28 07:03:10

我不了解 Java,所以这个答案与语言无关。

在我看来,无需在服务器上运行进程即可实现此目的的最简单方法是将所有数据存储在数据库中。

以下是需要完成的基本事项的简短列表:

  1. 需要一个包含用于身份验证的用户和密码列表的表
  2. 需要一个用于当前登录用户的表
    A. 需要最后一次联系的时间戳字段
  3. 当用户执行某些操作时,将最后一次联系字段更新为当前时间
  4. 如果用户的最后一次联系时间> 当前时间 + 2 分钟,然后他们将注销
  5. 客户端应用程序将需要定期向服务器发送消息以表示“我还在这里”
  6. 您需要找到一种方法来确定消息何时发送以及何时更新客户端显示已收到消息,这个我会留给你。

如果您仍然需要一些帮助,这里有一个 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:

  1. Need a table with a list of users and passwords for authentication
  2. Need a table for the currently logged in uses
    A. needs a time stamp field of the last contact
  3. When a users does something update the last contact field to the current time
  4. If the user' last contact time is > current time + 2 minutes then they are logged out
  5. client side application will need to send periodic messages to the server to say "Im still here"
  6. You'll need to find a way to determine when a message has been sent and when to update the client's display that a message has been received, this I will leave to you.

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.

2024-07-28 07:03:10

我编写了一个聊天引擎,它在后台有一个服务,所有内容都存储在数据库中,一个输入表单框架和一个接收 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.

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