Initial commit - ChatRoom project

This commit is contained in:
FOXeverx 2025-09-17 16:38:41 +08:00
commit a9e340bcb3
9 changed files with 481 additions and 0 deletions

170
ChatingRoom.java Normal file
View File

@ -0,0 +1,170 @@
package 大作业;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class ChatingRoom extends JFrame implements Runnable{
public JTextArea chat_Frame=new JTextArea();
private JScrollPane pane1=new JScrollPane(chat_Frame);
private JTextArea edit_Text=new JTextArea();
private JScrollPane pane2=new JScrollPane(edit_Text);
private JButton sent_Button=new JButton("发送");
private JButton return_Button=new JButton("退出聊天室");
public String text;
public String Name;
private String Ip;
private int option;
public boolean isClose;
public boolean alsent;
public ChatingRoom(String name,String IP,int option){
Name=name;
Ip=IP;
this.option=option;
isClose=false;
return_Button.setBackground(Color.red);
chat_Frame.setBorder(null);
chat_Frame.setOpaque(false);
pane1.setBorder(null);
pane1.setOpaque(false);
chat_Frame.setEditable(false);
setSize(800, 500);setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);setLocationRelativeTo(null);
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=0;c.gridy=0;
c.gridwidth=2;
c.ipady=200;
add(pane1, c);
c.gridx=0;c.gridy=1;
c.gridwidth=2;
c.weightx=3;c.weighty=3;
c.ipady=100;
add(pane2, c);
c.gridx=0;c.gridy=2;
c.gridwidth=1;
c.weightx=1;c.weighty=1;
c.ipady=10;
add(sent_Button, c);
c.gridx=1;c.gridy=2;
c.gridwidth=1;
c.weightx=1;c.weighty=1;
c.ipady=10;
add(return_Button, c);
return_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Log_In_frame log_In_frame=new Log_In_frame();
log_In_frame.setTitle("登录界面");
log_In_frame.setVisible(true);
isClose=true;
dispose();
}
});
if(option==2){
sent_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
chat_Frame.append(text+'\n');
edit_Text.setText("");
}
});
}else if(option==1){
sent_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
edit_Text.setText("");
}
});
}
}
public void run(){
if (option==2){
setVisible(true);
final int PORT=3030;
ServerSocket s=null;
try{
s=new ServerSocket(PORT);
System.out.println("启动服务器"+'\n'+s);
Socket socket=null;
while (true) {
try {
socket=s.accept();
ServerWorker serverWorker=new ServerWorker(socket,this);
ServerManager.getServetManager().add(serverWorker);
sent_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
text=Name+" "+edit_Text.getText();
alsent=true;
serverWorker.out.println(text);
}
});
} catch (IOException e1) {
System.out.println(e1);
System.out.println("获取客户端Scoket异常");
}
return_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
ServerManager.getServetManager().publishall(Name+"退出聊天室"+'\n'+"聊天室关闭");
}
});
}
} catch (IOException e1) {
System.out.println(e1);
System.out.println("ServerSocket创建异常");
} finally{
try {
if(s!=null)
s.close();
} catch (IOException e1) {
System.out.println(e1);
System.out.println("ServerSocket关闭异常");
}
}
}
if(option==1){
setVisible(true);
try {
InetAddress addr=InetAddress.getByName(Ip);
CilentSocketMultThread cilentSocketMultThread=new CilentSocketMultThread(addr, this);
sent_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
text=Name+" "+edit_Text.getText();
alsent=true;
// edit_Text.setText("");
cilentSocketMultThread.out.println(text);
}
});
return_Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
cilentSocketMultThread.out.println(Name+"退出了聊天室");
}
});
} catch (IOException e1) {
}
}
}
}

77
Client_Scoket.java Normal file
View File

@ -0,0 +1,77 @@
package 大作业;
import java.io.*;
import java.net.*;
class CilentSocketMultThread extends Thread{
private Socket socket;
private BufferedReader in;
public PrintWriter out;
private ChatingRoom chatingRoom;
public CilentSocketMultThread(InetAddress addr,ChatingRoom c)throws IOException{
chatingRoom=c;
try{
socket=new Socket(addr,Sever_Socket.PORT);
System.out.println("启动客户端:"+socket);
} catch(IOException exception){
System.out.println(exception);
System.out.println("创建客户端失败");
chatingRoom.chat_Frame.append("输入了无效的IP地址或该客户端未开启");
}
try {
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
out.println(chatingRoom.Name+"加入聊天室"+'\n');
start();
} catch (IOException e) {
System.out.println(e);
System.out.println("流异常");
try {
socket.close();
} catch (IOException exception) {
System.out.println(exception);
System.out.println("scoket关闭异常");
}
}
}
public void run(){
try {
while (true) {
String str=in.readLine();
chatingRoom.chat_Frame.append(str+'\n');
if(chatingRoom.isClose==true)
break;
}
} catch (IOException e) {
chatingRoom.chat_Frame.append("聊天室关闭");
System.out.println(e);
System.out.println("流异常");
try {
socket.close();
} catch (IOException e1) {
System.out.println(e1);
System.out.println("socket关闭异常");
}
}
}
}
public class Client_Scoket {
/*static final int MAX_THREAD=10;
public static void main(String[] args) throws IOException,InterruptedException {
InetAddress addr=InetAddress.getByName(null);
while(true){
if(CilentSocketMultThread.threadCount()<MAX_THREAD)
new CilentSocketMultThread(addr);
Thread.sleep(100);
}
}*/
}

28
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,28 @@
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git url: 'http://<服务器IP>:3000/<用户名>/ChatRoomProject.git', branch: 'master'
}
}
stage('Build Docker Image') {
steps {
script {
docker.build("chatroom:latest", ".")
}
}
}
stage('Deploy to k3s') {
steps {
sh '''
kubectl delete deployment chatroom --ignore-not-found
kubectl apply -f k8s-deployment.yaml
'''
}
}
}
}

58
Log_In_frame.java Normal file
View File

@ -0,0 +1,58 @@
package 大作业;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.*;
public class Log_In_frame extends JFrame{
private JLabel username_JLabel=new JLabel("用户名");
private JLabel InetAddress_JLable=new JLabel("连接IP");
private JTextField username_JTextField=new JTextField();
private JTextField InetAddress_JTextField=new JTextField();
private JButton button1=new JButton("连接");
private JButton button2=new JButton("建立主机");
public String username;
public String IP;
public int option;
public Log_In_frame(){
option=0;
setSize(300,300);
setLocationRelativeTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,3));
add(username_JLabel);add(username_JTextField);
add(InetAddress_JLable);add(InetAddress_JTextField);
add(button1);add(button2);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
username=username_JTextField.getText();
IP=InetAddress_JTextField.getText();
option=1;
ChatingRoom chatingRoom=new ChatingRoom(username,IP,option);
chatingRoom.setTitle(username);
new Thread(chatingRoom).start();
dispose();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
username=username_JTextField.getText();
IP=InetAddress_JTextField.getText();
option=2;
ChatingRoom chatingRoom=new ChatingRoom("(主机)"+username,IP,option);
chatingRoom.setTitle("(主机)"+username);
new Thread(chatingRoom).start();
dispose();
}
});
}
}

39
ServerManager.java Normal file
View File

@ -0,0 +1,39 @@
package 大作业;
import java.util.Vector;
public class ServerManager {
private ServerManager() {}
private static final ServerManager sm = new ServerManager();
public static ServerManager getServetManager() {
return sm;
}
Vector<ServerWorker> vector = new Vector<ServerWorker>();
public void add(ServerWorker cs) {
vector.add(cs);
}
public void remove(ServerWorker cs) {
vector.remove(cs);
}
public void publish(ServerWorker cs,String str) {
for (int i = 0; i < vector.size(); i++) {
ServerWorker serverWorker = vector.get(i);
if (!cs.equals(serverWorker)) {
serverWorker.out.println(str);
}
}
}
public void publishall(String str){
for (int i = 0; i < vector.size(); i++) {
ServerWorker serverWorker = vector.get(i);
serverWorker.out.println(str);
}
}
}

53
Sever_Socket.java Normal file
View File

@ -0,0 +1,53 @@
package 大作业;
import java.io.*;
import java.net.*;
class ServerWorker extends Thread{
private Socket socket;
public BufferedReader in;
public PrintWriter out;
private ChatingRoom chatingRoom;
public ServerWorker(Socket s,ChatingRoom c){
chatingRoom=c;
try{
socket=s;
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
start();
}catch(IOException exception){
System.out.println("scoket创建异常");
System.out.println(exception);
}
}
public void run(){
try{
while (true) {
String str=in.readLine();
chatingRoom.chat_Frame.append(str+'\n');
ServerManager.getServetManager().publish(this, str);
out.println(str);
if(chatingRoom.isClose==true)
break;
}
System.out.println("关闭...");
}catch(IOException exception){
ServerManager.getServetManager().remove(this);
System.out.println(exception);
System.out.println("流异常");
}finally{
try {
socket.close();
} catch (IOException e) {
System.out.println(e);
System.out.println("socket关闭异常");
}
}
}
}
public class Sever_Socket {
static final int PORT=3030;
}

12
dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM openjdk:17-slim
WORKDIR /app
# 拷贝所有源码
COPY . /app
# 编译所有 Java 文件
RUN javac *.java
# 默认启动服务端
CMD ["java", "mainApp"]

32
k8s-deployment.yaml Normal file
View File

@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: chatroom
spec:
replicas: 1
selector:
matchLabels:
app: chatroom
template:
metadata:
labels:
app: chatroom
spec:
containers:
- name: chatroom
image: chatroom:latest
ports:
- containerPort: 3030 # 服务器监听的端口
---
apiVersion: v1
kind: Service
metadata:
name: chatroom-service
spec:
selector:
app: chatroom
type: NodePort
ports:
- port: 3030
targetPort: 3030
nodePort: 30330 # 外部访问端口http://服务器IP:30330

12
mainApp.java Normal file
View File

@ -0,0 +1,12 @@
package 大作业;
// import 大作业.ChatingRoom;
// import 大作业.Log_In_frame;
public class mainApp {
public static void main(String[] args) {
Log_In_frame log_In_frame=new Log_In_frame();
log_In_frame.setTitle("登录界面");
log_In_frame.setVisible(true);
}
}