This commit is contained in:
2025-11-11 22:03:30 +08:00
parent 685bb0cebd
commit ff88c2c04a
947 changed files with 122829 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
package org.example;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequestUtil {
/**
* 向指定URL发送POST请求发送JSON数据
* @param urlStr 目标接口地址
* @param jsonData 发送的JSON字符串
* @return 响应内容
* @throws Exception 网络异常
*/
public static String postJson(String urlStr, String jsonData) throws Exception {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
try (OutputStream os = conn.getOutputStream()) {
os.write(jsonData.getBytes("UTF-8"));
}
int code = conn.getResponseCode();
if (code == 200) {
try (java.io.InputStream is = conn.getInputStream()) {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return new String(baos.toByteArray(), "UTF-8");
}
} else {
throw new RuntimeException("HTTP请求失败状态码: " + code);
}
}
/**
* 向指定URL发送GET请求
* @param urlStr 目标接口地址
* @return 响应内容
* @throws Exception 网络异常
*/
public static String get(String urlStr) throws Exception {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setDoInput(true);
int code = conn.getResponseCode();
if (code == 200) {
try (java.io.InputStream is = conn.getInputStream()) {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return new String(baos.toByteArray(), "UTF-8");
}
} else {
throw new RuntimeException("HTTP GET请求失败状态码: " + code);
}
}
}

View File

@@ -0,0 +1,59 @@
package org.example;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint("/ws")
public class WsServer {
private static final Set<Session> sessions = new CopyOnWriteArraySet<>();
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
System.out.println("WebSocket连接已建立: " + session.getId());
// 推送所有扫码枪列表给前端(使用修改后的方法名)
// String deviceListJson = Main.getAllDevicesJson();
// try {
// System.out.println("发送设备列表到前端: " + deviceListJson);
// session.getBasicRemote().sendText(deviceListJson);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
@OnMessage
public void onMessage(String message, Session session) throws IOException {
System.out.println("收到消息: " + message);
// 可以根据前端需求,在这里处理命令(比如刷新设备列表)
if ("refreshDevices".equals(message)) {
// String deviceListJson = Main.getAllDevicesJson();
// session.getBasicRemote().sendText(deviceListJson);
} else {
session.getBasicRemote().sendText("服务器已收到: " + message);
}
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
System.out.println("WebSocket连接已关闭: " + session.getId());
}
public static void broadcast(String message) {
for (Session session : sessions) {
if (session.isOpen()) {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}