Browse Source

Merge branch 'feature/client/exchange-messages-between-client-server' into 'main'

Exchange messages between client and server

See merge request fdai7332/java-chat!4
remotes/origin/feature/client/exchange-messages-between-client-server
fdai5595 11 months ago
parent
commit
fefdd1b4f1
  1. 44
      src/main/java/ChatClient.java

44
src/main/java/ChatClient.java

@ -2,17 +2,20 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.*;
import java.net.Socket; import java.net.Socket;
public class ChatClient extends JFrame implements KeyListener { public class ChatClient extends JFrame implements KeyListener {
private String address; private String address;
private int port; private int port;
private Socket connectionToServer; private Socket connectionToServer;
private BufferedReader fromServerReader;
private PrintWriter toServerWriter;
// GUI // GUI
private JTextArea outputTextArea; private JTextArea outputTextArea;
private JTextField inputTextField; private JTextField inputTextField;
private JScrollPane outputScrollPane;
public ChatClient(int port) { public ChatClient(int port) {
super("Chat"); super("Chat");
@ -29,11 +32,13 @@ public class ChatClient extends JFrame implements KeyListener {
outputTextArea.setEditable(false); outputTextArea.setEditable(false);
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat"));
outputScrollPane = new JScrollPane(outputTextArea);
inputTextField = new JTextField(); inputTextField = new JTextField();
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben"));
inputTextField.addKeyListener(this); inputTextField.addKeyListener(this);
add(outputTextArea, BorderLayout.CENTER);
add(outputScrollPane, BorderLayout.CENTER);
add(inputTextField, BorderLayout.SOUTH); add(inputTextField, BorderLayout.SOUTH);
setVisible(true); setVisible(true);
@ -43,9 +48,39 @@ public class ChatClient extends JFrame implements KeyListener {
private void receiveMessages() { private void receiveMessages() {
try { try {
connectionToServer = new Socket(address, port); connectionToServer = new Socket(address, port);
fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream()));
toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream()));
initGui(); initGui();
} catch (Exception e) {
while (true) {
String message = fromServerReader.readLine();
outputTextArea.append(message + "\n");
outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum());
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen.");
dispose();
} finally {
if (null != connectionToServer) {
try {
connectionToServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fromServerReader) {
try {
fromServerReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != toServerWriter) {
toServerWriter.close();
}
} }
} }
@ -59,7 +94,8 @@ public class ChatClient extends JFrame implements KeyListener {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String message = inputTextField.getText(); String message = inputTextField.getText();
if (!message.isEmpty()) { if (!message.isEmpty()) {
// Senden die Nachricht an Server, wenn der erstellt wird
toServerWriter.println(message);
toServerWriter.flush();
inputTextField.setText(""); inputTextField.setText("");
} }
} }

Loading…
Cancel
Save