diff --git a/.gitignore b/.gitignore index bdd4439..e4316fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pdf /out/ /Homeworks/ +*.zip diff --git a/.idea/libraries/sqlite_jdbc_3_21_0.xml b/.idea/libraries/sqlite_jdbc_3_21_0.xml new file mode 100644 index 0000000..7ab8ea7 --- /dev/null +++ b/.idea/libraries/sqlite_jdbc_3_21_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Java2.iml b/Java2.iml index c90834f..050079b 100644 --- a/Java2.iml +++ b/Java2.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/JavaFX_chat.db b/JavaFX_chat.db new file mode 100644 index 0000000..4c5da40 Binary files /dev/null and b/JavaFX_chat.db differ diff --git a/src/ru/geekbrains/java2/lesson_06/Classwork/client/Controller.java b/src/ru/geekbrains/java2/lesson_06/Classwork/client/Controller.java new file mode 100644 index 0000000..ff76fa0 --- /dev/null +++ b/src/ru/geekbrains/java2/lesson_06/Classwork/client/Controller.java @@ -0,0 +1,74 @@ +package ru.geekbrains.java2.lesson_06.Classwork.client; + +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.URL; +import java.util.ResourceBundle; + +public class Controller implements Initializable { + public static final String SERVER_IP = "localhost"; + public static final int SERVER_PORT = 8189; + + @FXML + public TextArea chatArea; + @FXML + public TextField msgField; +// public Button sendButton; + + private Socket socket; + private DataOutputStream out; + + @Override + public void initialize(URL location, ResourceBundle resources) { + try { + socket = new Socket(SERVER_IP, SERVER_PORT); + DataInputStream in = new DataInputStream(socket.getInputStream()); + out = new DataOutputStream(socket.getOutputStream()); + Thread thread = new Thread(() -> { + String msg = ""; + try { + while (!msg.equals("echo answer: /end")) { + msg = in.readUTF(); + chatArea.appendText(msg + "\n"); + } + chatArea.appendText("соединение разорвано\n"); + } catch (IOException e) { + System.err.println("Сервер рвзорвал соединение"); + e.printStackTrace(); +// System.exit(0); + } + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + thread.setDaemon(true); + thread.start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendMsg() { + if (!msgField.getText().equals("")) + try { + out.writeUTF(msgField.getText()); + out.flush(); + msgField.clear(); + msgField.requestFocus(); + } catch (IOException e) { + chatArea.appendText("нет связи с сервером\n"); + } + } + +} + diff --git a/src/ru/geekbrains/java2/lesson_06/Classwork/client/Main.java b/src/ru/geekbrains/java2/lesson_06/Classwork/client/Main.java new file mode 100644 index 0000000..4c4e59b --- /dev/null +++ b/src/ru/geekbrains/java2/lesson_06/Classwork/client/Main.java @@ -0,0 +1,23 @@ +package ru.geekbrains.java2.lesson_06.Classwork.client; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) throws Exception{ + Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); + primaryStage.setTitle("Geek JavaFX chat"); + primaryStage.setScene(new Scene(root, 400, 600)); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/src/ru/geekbrains/java2/lesson_06/Classwork/client/sample.fxml b/src/ru/geekbrains/java2/lesson_06/Classwork/client/sample.fxml new file mode 100644 index 0000000..13236a6 --- /dev/null +++ b/src/ru/geekbrains/java2/lesson_06/Classwork/client/sample.fxml @@ -0,0 +1,14 @@ + + + + + + +