From a9e340bcb379402124f93c7f1a6d4ca78c3b0f5f Mon Sep 17 00:00:00 2001 From: FOXeverx <3541692950@qq.com> Date: Wed, 17 Sep 2025 16:38:41 +0800 Subject: [PATCH] Initial commit - ChatRoom project --- ChatingRoom.java | 170 ++++++++++++++++++++++++++++++++++++++++++++ Client_Scoket.java | 77 ++++++++++++++++++++ Jenkinsfile | 28 ++++++++ Log_In_frame.java | 58 +++++++++++++++ ServerManager.java | 39 ++++++++++ Sever_Socket.java | 53 ++++++++++++++ dockerfile | 12 ++++ k8s-deployment.yaml | 32 +++++++++ mainApp.java | 12 ++++ 9 files changed, 481 insertions(+) create mode 100644 ChatingRoom.java create mode 100644 Client_Scoket.java create mode 100644 Jenkinsfile create mode 100644 Log_In_frame.java create mode 100644 ServerManager.java create mode 100644 Sever_Socket.java create mode 100644 dockerfile create mode 100644 k8s-deployment.yaml create mode 100644 mainApp.java diff --git a/ChatingRoom.java b/ChatingRoom.java new file mode 100644 index 0000000..89f1aa1 --- /dev/null +++ b/ChatingRoom.java @@ -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) { + } + + + } + } + + + +} diff --git a/Client_Scoket.java b/Client_Scoket.java new file mode 100644 index 0000000..8e44187 --- /dev/null +++ b/Client_Scoket.java @@ -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():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 + ''' + } + } + } +} diff --git a/Log_In_frame.java b/Log_In_frame.java new file mode 100644 index 0000000..3b51e1e --- /dev/null +++ b/Log_In_frame.java @@ -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(); + + } + }); + } + +} diff --git a/ServerManager.java b/ServerManager.java new file mode 100644 index 0000000..b18fe9d --- /dev/null +++ b/ServerManager.java @@ -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 vector = new Vector(); + + 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); + } + } +} \ No newline at end of file diff --git a/Sever_Socket.java b/Sever_Socket.java new file mode 100644 index 0000000..6ae1fd6 --- /dev/null +++ b/Sever_Socket.java @@ -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; +} diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..a5db589 --- /dev/null +++ b/dockerfile @@ -0,0 +1,12 @@ +FROM openjdk:17-slim + +WORKDIR /app + +# 拷贝所有源码 +COPY . /app + +# 编译所有 Java 文件 +RUN javac *.java + +# 默认启动服务端 +CMD ["java", "mainApp"] diff --git a/k8s-deployment.yaml b/k8s-deployment.yaml new file mode 100644 index 0000000..5153c4d --- /dev/null +++ b/k8s-deployment.yaml @@ -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) diff --git a/mainApp.java b/mainApp.java new file mode 100644 index 0000000..6aa9d23 --- /dev/null +++ b/mainApp.java @@ -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); + } +} \ No newline at end of file