-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
490 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: com.example.client.ClientWindow | ||
|
Oops, something went wrong.