Java 即时通讯问题

发布于 2025-01-02 19:46:13 字数 426 浏览 2 评论 0原文

我正在寻找用 Java 构建一个即时通讯工具。

  1. 客户端将连接到服务器进行登录。
  2. 他们将​​开始与一个或多个其他客户端进行对话。
  3. 然后,他们将消息发布到服务器,服务器将消息转发给所有客户端。

当用户发布消息或登录时,客户端需要不断更新。

所以在我看来,客户端需要在单独的线程中运行服务器本身,以便主服务器可以向其发送内容。否则,客户端将必须每隔 xyz 秒轮询一次主服务器以获取最新更新。无论如何,这将需要一个单独的线程,因为这纯粹是为了获取更新,而“主”线程将用于客户端启动诸如发布消息/邀请其他人进行对话等操作时......

所以任何人都建议如何写这个即时通讯工具?使连接成为客户端和服务器都充当服务器的“双向”连接,听起来是个好主意吗?或者民意调查是更好的选择吗?有人知道 IRC 协议是如何做到这一点的吗?

I am looking to build an instant messenger in Java.

  1. Clients will connect to the server to log in.
  2. They will start a conversation with one or more other clients.
  3. They will then post messages to the server that will relay the messages to all the clients.

The client needs to be continually updated when users post messages or log in.

so the way I see it, the client needs to run a server itself in a separate thread so that the main server can send stuff to it. Otherwise the client will have to the poll the main server every xyz seconds to get the latest updates. And that would need a separate thread anayway, as that would be purely for getting updates whereas the 'main' thread would be used for when the client initiates actions such as posting messages/inviting others to conversations etc...

So anyone recommendations on how to write this instant messenger? Does it sound like a good idea to make the connection a 'two-way' connection where both the client and server act as servers? Or is polling a better option? Anyone know how the IRC protocol does this?

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

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

发布评论

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

评论(4

比忠 2025-01-09 19:46:13

拥有 2 个连接并没有真正的优势,除非它们可以独立处理(例如接收/发送文件通常在单独的连接中完成)。连接本身已经是双向通信通道,因此它可以用于发送和接收消息、事件等。您不需要轮询服务器,因为客户端能够维持持久连接并只需等待数据出现( (可选地定期发送类似于 PING 的消息以确保连接处于活动状态)。

IRC 使用与服务器的单个连接来交换文本命令。例如主要命令之一:

PRIVMSG <msgtarget> <message>

该命令可以由客户端或服务器发起。客户端发送 PRIVMSG 来通知它想要将消息传送到一个或多个目的地(在 IRC 中为用户或通道)。服务器的任务是正确地将此消息广播给适当的客户端。

There's no real advantage of having 2 connections unless they can be handled independently (for example receiving / sending a file usually done in a separate connection). A connection itself is already a two-way communication channel so it can be used to both send and receive messages, events etc. You don't need to poll server since client is able to maintain persistent connection and just wait for data to appear (optionally sending periodic PING-like message to ensure connection is alive).

IRC uses a single connection to server to exchange text commands. For example one of the main commands:

PRIVMSG <msgtarget> <message>

This command can be originated either by client or by server. Client sends PRIVMSG to notify that it wants to deliver message to one or more destination (in IRC this either user(s) or channel(s)). Server's task here is to properly broadcast this message to appropriate clients.

七色彩虹 2025-01-09 19:46:13

如果您使用原始输入输出流,那么这是一个很好的方法。您在客户端创建一个线程,其行为方式与服务器线程类似 - 等待任何传入的更新,并在更新时更新客户端。但我不会称其为服务器。因此,理想情况下,您应该有 2 个 TCP/UDP 连接,一个用于客户端发出的请求,另一个用于通知客户端服务器更改。

企业环境中的这个解决方案可能会通过某种消息传递框架(例如 Spring Integration)来完成,但如果深入挖掘,它本质上与您提到的方式类似。

If you're using raw InputOutput streams then yes this is a good way of doing it. You create one thread on the clientside that acts in a similar fashion as the server thread - waits for any incoming updates and when it does it updates the client. I wouldn't call it a server though. So you'd ideally have 2 TCP/UDP connections one for requests made by the client and one to notify the client of server changes.

This solution in an enterprise environment would probably be done through some kind of messaging framework such as Spring Integration but dig deep enough and it will essentially be a similar way to how you mentioned.

旧街凉风 2025-01-09 19:46:13

您是否需要完全自定义的协议,或者使用 XMPP 就足够了吗?有几个实现 XMPP 的开源库。
http://xmpp.org/xmpp-software/libraries/

例如http://www.igniterealtime.org/projects/smack/

Do you need a fully custom protocol or would it be sufficient to use the XMPP? There are several open source libraries implementing XMPP.
http://xmpp.org/xmpp-software/libraries/

e.g. http://www.igniterealtime.org/projects/smack/

好倦 2025-01-09 19:46:13

对于我来说,为了开发即时消息服务,我将使用 websocket 协议而不是普通的 java 套接字,因为普通的套接字不能很好地与 HTTP 协议一起工作,而且一些网络提供商和防火墙禁止自定义端口。如果你用普通的socket开发,你的服务将无法被Web客户端访问。

您是否打算自己开发即时通讯服务?使用其他协议(例如 Jabber 怎么样?

For me, to develop instant messaging service, I will use websocket protocol instead of normal java socket because the normal socket can not work well with HTTP protocol and moreover some network providers and firewalls banned custom ports. If you develop it in normal socket, your service could not be accessed by web clients.

Did you plan to develop the instant messaging service yourself? How about using other protocols such as Jabber?

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