一位客户与另一位客户聊天
我创建了一个简单的Java聊天程序,它提供一个服务器和多个客户端。 但我只能让它在所有客户端都可以交谈的地方(消息已发送给所有客户端)。 除了已进行的公开聊天之外,我还需要进行私人聊天。 例如:computerA只想与computerB聊天,但computerA仍然可以与所有客户端通话。我该怎么做?
I have created a simple Java chat program, which provides one server and multiple clients.
But I only can make it where all clients can talk (the messages was sent to all clients).
I need to make a private chat beside the public chat I've made.
E.g: computerA just wants to chat with computerB, but computerA still can talk with all clients. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是稍微修改您的协议,以包含一些信息,无论您发送的消息是公共消息(每个人都可以看到它)还是私有消息(仅用户 A 和用户 B 通话)。
有了这些信息,在服务器中,每当您找到私人消息时,只需将其发送到唯一期望该消息的目的地即可。不要尝试做一些点对点的事情,因为一路上你会遇到很多问题。
该解决方案的其他小变化是支持聊天服务器上的“私人房间”。但这实施起来会稍微复杂一些。
The easiest way to do it is to modify your protocol a little bit to include some information whether the message you are sending is a public (everyone can see it) or a private one (just user A and user B talking).
With that information in hand, in the server, whenever you find a private message, just send it to the one and only destination expecting that message. Do not try to do some peer to peer stuff because you will encounter many problems along the way.
Other small variation of this solution would be supporting "private rooms" on your chat server. But that will be a little bit more complicated to implement.