2 Commits f8bca6159d ... 2316733116

Autor SHA1 Mensaje Fecha
  544319623@qq.com 2316733116 Merge branch 'master' of http://git.dashitech.com/chenxiao/hsmsV1-CallSocket hace 6 meses
  544319623@qq.com eb5f4d5750 测试工具 hace 6 meses
Se han modificado 1 ficheros con 186 adiciones y 0 borrados
  1. 186 0
      src/main/java/com/dashitech/callcenter/init/ChatWindow.java

+ 186 - 0
src/main/java/com/dashitech/callcenter/init/ChatWindow.java

@@ -0,0 +1,186 @@
1
+import javax.swing.*;
2
+import java.awt.*;
3
+import java.awt.event.ActionEvent;
4
+import java.awt.event.ActionListener;
5
+import java.awt.event.MouseAdapter;
6
+import java.awt.event.MouseEvent;
7
+import java.io.BufferedReader;
8
+import java.io.IOException;
9
+import java.io.InputStreamReader;
10
+import java.io.OutputStream;
11
+import java.net.Socket;
12
+import java.nio.charset.StandardCharsets;
13
+
14
+public class ChatWindow extends JFrame {
15
+    private JTextArea receivedMessagesArea;
16
+    private JTextField messageField;
17
+    private JButton sendButton;
18
+    private JTextField ipField;
19
+    private JTextField portField;
20
+    private JButton connectButton;
21
+    private JButton disconnectButton;
22
+    private JLabel connectionStatusLabel; // 连接状态标签
23
+    private JTextArea sentMessagesArea;
24
+    //private JTextArea chatArea;
25
+
26
+    private Socket socket;
27
+    private BufferedReader input;
28
+    private OutputStream output;
29
+
30
+    public ChatWindow() {
31
+        setTitle("socket test Window1");
32
+        setSize(800, 500);
33
+        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34
+
35
+        JPanel contentPane = new JPanel(new BorderLayout());
36
+
37
+        receivedMessagesArea = new JTextArea();
38
+        receivedMessagesArea.setPreferredSize(new Dimension(400, receivedMessagesArea.getPreferredSize().height));
39
+       // receivedMessagesArea.set
40
+        receivedMessagesArea.setEditable(false);
41
+        contentPane.add(new JScrollPane(receivedMessagesArea), BorderLayout.WEST);
42
+
43
+        sentMessagesArea = new JTextArea(); // 初始化已发送消息的文本区域
44
+        sentMessagesArea.setPreferredSize(new Dimension(400, sentMessagesArea.getPreferredSize().height));
45
+        // 设置已发送消息的文本区域为只读
46
+        sentMessagesArea.setEditable(false);
47
+        // 重新发送功能的实现,例如在已发送消息区域的双击事件中进行重新发送
48
+
49
+        //contentPane.add(new JScrollPane(sentMessagesArea), BorderLayout.EAST);
50
+        contentPane.add(new JScrollPane(sentMessagesArea), BorderLayout.EAST);
51
+        setContentPane(contentPane);
52
+
53
+        JPanel messagePanel = new JPanel(new BorderLayout());
54
+        messageField = new JTextField("{\"cmd\":\"loginreq\",\"ipaddr\":\"192.168.25.37\",\"serialNo\":\"23450501024971\",\"recordNumber\":4,\"webport\":80,\"filestoretype\":0}");
55
+        sendButton = new JButton("Send");
56
+        sendButton.addActionListener(new ActionListener() {
57
+            @Override
58
+            public void actionPerformed(ActionEvent e) {
59
+                sendMessage(messageField.getText());
60
+            }
61
+        });
62
+        messagePanel.add(messageField, BorderLayout.CENTER);
63
+        messagePanel.add(sendButton, BorderLayout.EAST);
64
+        add(messagePanel, BorderLayout.SOUTH);
65
+
66
+
67
+        JPanel connectPanel = new JPanel();
68
+        ipField = new JTextField("192.168.3.111",10);
69
+        portField = new JTextField("8087",5);
70
+        connectButton = new JButton("连接");
71
+        disconnectButton = new JButton("断开");
72
+        connectionStatusLabel = new JLabel("未连接");
73
+
74
+        connectButton.addActionListener(new ActionListener() {
75
+            @Override
76
+            public void actionPerformed(ActionEvent e) {
77
+                connectToServer();
78
+            }
79
+        });
80
+
81
+        disconnectButton.addActionListener(new ActionListener() {
82
+            @Override
83
+            public void actionPerformed(ActionEvent e) {
84
+                disconnectFromServer();
85
+            }
86
+        });
87
+
88
+        connectPanel.add(new JLabel("Server IP:"));
89
+        connectPanel.add(ipField);
90
+        connectPanel.add(new JLabel("Port:"));
91
+        connectPanel.add(portField);
92
+        connectPanel.add(connectButton);
93
+        connectPanel.add(disconnectButton);
94
+        connectPanel.add(connectionStatusLabel);
95
+        add(connectPanel, BorderLayout.NORTH);
96
+
97
+        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
98
+        setVisible(true);
99
+    }
100
+
101
+    public void connectToServer() {
102
+        String ip = ipField.getText();
103
+        int port = Integer.parseInt(portField.getText());
104
+
105
+        try {
106
+            socket = new Socket(ip, port);
107
+            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
108
+            output = socket.getOutputStream();
109
+
110
+            SwingUtilities.invokeLater(() -> {
111
+                connectionStatusLabel.setText("已连接");
112
+            });
113
+
114
+            Thread receiveThread = new Thread(() -> {
115
+                try {
116
+                    char[] buffer = new char[1024]; // 用于存储读取的消息
117
+                    int bytesRead;
118
+                    while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) {
119
+                        String message = new String(buffer, 0, bytesRead);
120
+                        receivedMessagesArea.append(message+"\n");
121
+                    }
122
+                } catch (IOException e) {
123
+                    e.printStackTrace();
124
+                }
125
+            });
126
+            receiveThread.start();
127
+        } catch (IOException e) {
128
+            e.printStackTrace();
129
+            // 更新连接状态为"连接断开"
130
+            SwingUtilities.invokeLater(() -> {
131
+                connectionStatusLabel.setText("已断开");
132
+            });
133
+        }
134
+    }
135
+
136
+    public void disconnectFromServer() {
137
+        try {
138
+            if (socket != null) {
139
+                socket.close();
140
+                socket = null;
141
+                input = null;
142
+                output = null;
143
+            }
144
+            SwingUtilities.invokeLater(() -> {
145
+                connectionStatusLabel.setText("已断开");
146
+            });
147
+        } catch (IOException e) {
148
+            e.printStackTrace();
149
+            // 更新连接状态为"连接断开"
150
+            SwingUtilities.invokeLater(() -> {
151
+                connectionStatusLabel.setText("已断开");
152
+            });
153
+        }
154
+    }
155
+
156
+    public void sendMessage(String message) {
157
+        //String message =
158
+        sentMessagesArea.append(message + "\n");
159
+        try {
160
+            String header = message.length() + ""; // 根据实际情况替换为包头字符数
161
+            byte[] requestData = message.getBytes(StandardCharsets.UTF_8); // 根据实际情况替换为请求字节数据
162
+
163
+            // 发送包头字符数
164
+            output.write(header.getBytes(StandardCharsets.UTF_8));
165
+
166
+            // 发送请求数据
167
+            output.write(requestData);
168
+            //output.write(message.getBytes());
169
+            //output.write('\n');
170
+            output.flush();
171
+            messageField.setText("");
172
+        } catch (IOException e) {
173
+            e.printStackTrace();
174
+            // 更新连接状态为"连接断开"
175
+            SwingUtilities.invokeLater(() -> {
176
+                connectionStatusLabel.setText("Disconnected");
177
+            });
178
+        }
179
+    }
180
+
181
+    public static void main(String[] args) {
182
+        SwingUtilities.invokeLater(() -> {
183
+            ChatWindow chatWindow = new ChatWindow();
184
+        });
185
+    }
186
+}