I am writing a chat application in Java swing which has a custom protocol and server backend using sockets. I am in the process of making the ClientSide connection handler. 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. Each JPanel needs the connection and the connection should be maintained as long as the app is functioning. 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? And would the referece also work if it was on a separate thread like a connection thread with a 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)
Aucun commentaire:
Enregistrer un commentaire