Skip to content

Commit

Permalink
Commit #1
Browse files Browse the repository at this point in the history
Загрузка простого пользовательского чата
  • Loading branch information
NikolayShvedov committed Sep 17, 2020
1 parent 31100ca commit 5d12c8d
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .idea/artifacts/client_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 144 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions client/client.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="13.0.1" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="network" />
</component>
</module>
110 changes: 110 additions & 0 deletions client/src/com/example/client/ClientWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.example.client;

import com.example.network.TCPConnection;
import com.example.network.TCPConnectionEvents;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
* Клиентское окно
* SERVER_HOST - адрес сервера
* PORT - порт
* WIDTH - ширина окна
* HEIGHT - высота окна
* chatWindow - окно чата
* nickName - поле для ввода имени клиента
* nick - метка для указания на поле ввода имени
* message - метка для указания на поле ввода сообщения
* inputMessage - поле для написания и отправки сообщения
* connection - соединение с сервером
*/
public class ClientWindow extends JFrame implements ActionListener, TCPConnectionEvents {

private static final String SERVER_HOST = "localhost";
private static final int PORT = 3443;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ClientWindow(600, 400);
}
});
}

private final JTextArea chatWindow = new JTextArea();
private final JTextField nickName = new JTextField();
private final JTextField inputMessage = new JTextField();

private TCPConnection connection;

private ClientWindow(int WIDTH, int HEIGHT){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setAlwaysOnTop(true);

chatWindow.setEditable(false);
chatWindow.setLineWrap(true);
add(chatWindow, BorderLayout.CENTER);

inputMessage.addActionListener(this);
add(nickName, BorderLayout.NORTH);
add(inputMessage, BorderLayout.SOUTH);

setVisible(true);
try {
connection = new TCPConnection(this, SERVER_HOST, PORT);
} catch (IOException e) {
printMessage("Connection exception: " + e);
}
}


@Override
public void actionPerformed(ActionEvent e) {
String msg = inputMessage.getText();
if (msg.equals(""))
{
return;
}
inputMessage.setText(null);
connection.sendMessage(nickName.getText() + ": " + msg);
}

@Override
public void onConnectionReady(TCPConnection tcpConnection) {
printMessage("Connection ready...");
}

@Override
public void onReceiveString(TCPConnection tcpConnection, String line) {
printMessage(line);
}

@Override
public void onDisconnect(TCPConnection tcpConnection) {
printMessage("Connection close");

}

@Override
public void onException(TCPConnection tcpConnection, Exception e) {
printMessage("Connection exception: " + e);
}

private synchronized void printMessage(String message){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
chatWindow.append(message + "\n");
chatWindow.setCaretPosition(chatWindow.getDocument().getLength());
}
});
}

}
11 changes: 11 additions & 0 deletions network/network.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 3 additions & 0 deletions network/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.example.client.ClientWindow

Loading

0 comments on commit 5d12c8d

Please sign in to comment.