简体   繁体   English

在多屏Swing应用中将JAVA包装插座作为Singleton类

[英]JAVA Wrapping Socket as a Singleton class in a multiscreen Swing app

I am writing a chat application in Java swing which has a custom protocol and server backend using sockets. 我正在用Java swing编写一个聊天应用程序,该应用程序具有自定义协议和使用套接字的服务器后端。 I am in the process of making the ClientSide connection handler. 我正在制作ClientSide连接处理程序。 Here is my code up to this point: 到目前为止,这是我的代码:

package Client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

import Common.Message;

public class ConnectionHandler {
    Socket socket;
    DataInputStream input;
    DataOutputStream output;

    public ConnectionHandler() throws UnknownHostException, IOException {
        socket = new Socket(Client.HOST_INET,Client.PORT);
        input = new DataInputStream(socket.getInputStream());
        output = new DataOutputStream(socket.getOutputStream());
    }

    public void sendMessage(Message message) throws IOException {
        output.writeUTF(Message.disasseble(message));
    }

    public Message getMessage() throws IOException {
        String message;
        message = input.readUTF();
        return Message.assemble(message);   
    }

    public void closeConnection() {

    }

    private void reconect(){

    }
}

The app is composed of a CardLauut holding the Login, Registration and and Chat JPanels. 该应用程序由一个CardLauut组成,该CardLauut包含登录,注册和聊天JPanels。 Each JPanel needs the connection and the connection should be maintained as long as the app is functioning. 每个JPanel需要连接,并且只要应用程序正常运行,就应保持连接。 Login screen sends Login message/answer the same with Registration and of course the Chat screen. 登录屏幕发送与注册相同的登录消息/答案,当然还有聊天屏幕。

QUESTION Should I use a singleton design pattern for the connectionHanler? 问题我应该对connectionHanler使用单例设计模式吗? And would the referece also work if it was on a separate thread like a connection thread with a que? 如果裁判位于诸如带有que的连接线程之类的单独线程上,裁判也会起作用吗? (I know that I need to ping the server every now and then or connection can be lost so I need to reconect and let the UI know) (我知道我需要不时对服务器进行ping操作,否则连接可能会丢失,因此我需要重新检查并让UI知道)

It makes sense to keep ConnectionHandler singleton for the following reasons: 出于以下原因,保持ConnectionHandler单例是有意义的:

  • You can manage connection state centrally, inside the ConnectionHandler instance. 您可以在ConnectionHandler实例内部集中管理连接状态。 For eg, don't send a chat message if user isn't logged in. 例如,如果用户未登录,请不要发送聊天消息。
  • Allows messages to be sent in sequence. 允许按顺序发送消息。 If you don't use Singleton pattern, it is theoretically possible that messages can be sent out of order in a multi-threaded application. 如果您不使用Singleton模式,则理论上可能会在多线程应用程序中无序发送消息。
  • Decrease load on server, because it only has one socket connection per client. 减少服务器上的负载,因为每个客户端只有一个套接字连接。

Holding the connection in a singleton has one additional advantage: you will very likely want to run all of your network communication in dedicated Thread. 将连接保持为单一状态还具有另一个优点:您很可能希望在专用线程中运行所有网络通信。 A singleton can greatly help there. 单身人士可以极大地帮助那里。

Also all Connection related properties (logged in?, transport encryption, ...) can be handled on a „per connection“ base. 同样,所有与连接相关的属性(已登录?,传输加密等)都可以在“每个连接”的基础上进行处理。

Handling out-of-band communication („log out“ or notifications while the Socket is busy) should be considered beforehand. 应事先考虑处理带外通信(“ Socket繁忙时注销”或通知)。

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

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