简体   繁体   English

使用Swing的Java聊天应用程序(概念性)

[英]Java chat application using Swing (Conceptual)

I want to write a chat application in Java using Swing as an interface. 我想使用Swing作为接口以Java编写聊天应用程序。

I have come up with an idea (with the help of MadProgrammer ), but I am not sure whether it is the best way to go about this. (在MadProgrammer的帮助下)我提出了一个想法,但是我不确定这是否是实现此目标的最佳方法。

There are two Blockinqueue queues in the main Thread, one for incoming messages and one for outgoing messages. main线程中有两个Blockinqueue队列,一个用于传入消息,一个用于传出消息。

There are four threads, two for outgoing and two for incoming messages, one each to handle the GUI and the socket. 有四个线程,两个用于传出消息,两个用于传入消息,每个线程用于处理GUI和套接字。

Threads for outgoing messages: 外发消息的线程:

  1. ActionListener (Swing) : Is triggered when user clicks "send" in GUI. ActionListener (Swing) :当用户在GUI中单击“发送”时触发。 The thread adds the new message to the Outgoing Queue and triggers notifyAll() on it. 该线程将新消息添加到传出队列,并在其上触发notifyAll()
  2. socketOutgoing : Has access to socket. socketOutgoing :可以访问套接字。 Sleeps, with wait() on Outgoing Queue, until it is notified. 在待发队列上使用wait()进行睡眠,直到收到通知。 Sends new messages in Outgoing Queue through socket, then goes back to sleep again. 通过套接字在“传出队列”中发送新消息,然后再次进入睡眠状态。

Threads for incoming messages: 传入消息的线程:

  1. socketIncoming: Has access to socket. socketIncoming:可以访问套接字。 Checks continuously for new message in socket (how?). 不断检查套接字中是否有新消息(如何?)。 When there is a new message, adds it to the Outgoing Queue and triggers notifyAll() on it. 当有新消息时,将其添加到传出队列中并在其上触发notifyAll()
  2. Swingworker displayIncoming : Sleeps, with wait() on Incoming Queue, until it is notified. Swingworker displayIncoming :在传入队列上与wait()一起wait() ,直到收到通知。 Displays new messages in GUI, then goes back to sleep again. 在GUI中显示新消息,然后再次进入睡眠状态。

While this would theoretically work, it seems a bit messy (and unreliable) to have four threads for this. 尽管从理论上讲这是可行的,但为此有四个线程似乎有些混乱(并且不可靠)。

Is there a more practical solution? 有更实用的解决方案吗?

Note to future readers: My description of socketIncoming was misguided: It is not possible to "check continuously for new message in socket". 将来的读者注意:我对socketIncoming描述被误导了:无法“连续检查套接字中是否有新消息”。

When you call ObjectInputStream#readObject() , and there is no new message, it simply waits, or "blocks", until a new messages arrives. 当您调用ObjectInputStream#readObject() ,没有新消息,它只是等待或“阻塞”,直到新消息到达为止。 There is no way to check beforehand whether a new message has arrived. 无法预先检查是否有新消息到达。 See this question . 看到这个问题

If I was thinking about doing something like I would probably set up two queues, an outgoing and incoming queue. 如果我正在考虑做类似的事情,我可能会设置两个队列,即传出队列和传入队列。 These would be used to "stage" messages. 这些将用于“登台”消息。

The idea being that outgoing messages would be placed into the outgoing queue and when Thread was able to, it would pop off the next message and send it. 这个想法是将传出消息放入传出队列中,并且当Thread能够执行时,它将弹出下一条消息并将其发送。 When the queue was empty, it would simply "wait" until a new message become available. 当队列为空时,它将简单地“等待”直到有新消息可用。

The concept would work in reverse for the incoming queue. 对于传入队列,该概念将反向工作。 The Thread would read a message and push it onto the incoming queue. Thread将读取一条消息并将其推送到传入队列中。

Some other process (possibly a SwingWorker ) would be monitoring the queue and pop the next message of it and re-sync it with the GUI. 其他一些过程(可能是SwingWorker )将监视队列并弹出队列的下一条消息,并将其与GUI重新同步。

You might find Concurrency in Swing of some use. 您可能会在Swing中发现并发的某些用途。

How the underlying protocol actually worked would dictate a lot more of the details though 虽然底层协议的实际工作方式将决定更多细节

for a simple chat application you shall have two parts 对于简单的聊天应用程序,您应分为两个部分

  1. Client part 客户部分
  2. Server part. 服务器部分。

Now, you have to decide which protocol you want to use for your communication [Tcp] or [Udp]. 现在,您必须确定要使用哪种协议进行通信[Tcp]或[Udp]。 Though your message transmission should be reliable so you have to use java Tcp ServerSocket. 尽管您的消息传输应该可靠,所以您必须使用Java Tcp ServerSocket。 your server will be multi-threaded means for each client connect with server will have separate thread to handle all message communication from that client. 您的服务器将是多线程的,这意味着与服务器连接的每个客户端将具有单独的线程来处理来自该客户端的所有消息通信。

For Client side, it will have GUI componenet and one dedicated thread to receive message from server. 对于客户端,它将具有GUI组件和一个专用线程来接收来自服务器的消息。 when you want to send message to any user, just pass the message and send through client socket. 当您想向任何用户发送消息时,只需传递消息并通过客户端套接字发送即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM