添加 Session id

This commit is contained in:
2025-10-20 22:06:30 +08:00
parent 775bd96417
commit 79fe209d4f

View File

@@ -68,12 +68,6 @@ public class ChatWebSocketHandler {
*/
private final Map<String, WebSocket> cacheWebSocket = new ConcurrentHashMap<>();
/**
* 为每个会话维护分片缓存(线程安全,支持多用户)
*/
private final ConcurrentHashMap<String, List<byte[]>> fragmentCache = new ConcurrentHashMap<>();
// 语音文件保存目录
private static final String VOICE_STORAGE_DIR = "/voice_files/";
@@ -98,6 +92,7 @@ public class ChatWebSocketHandler {
@OnOpen
public void onOpen(Session session, @PathParam("clientId") String clientId) {
log.info("WebSocket 链接已建立:{}", clientId);
log.info("WebSocket session 链接已建立:{}", session.getId());
cacheClientTts.put(clientId,new String());
//初始化STT流式语音转换文本的socket链接
createWhisperRealtimeSocket(clientId);
@@ -116,26 +111,14 @@ public class ChatWebSocketHandler {
if("done".equals(resultFlag)){
log.info("1、开始处理时间:{}",System.currentTimeMillis()/1000);
// //开始合并语音流
// List<byte[]> fragments = fragmentCache.get(clientId);
// // 合并所有分片为完整语音数据
// byte[] fullVoiceData = mergeFragments(fragments);
// // 生成唯一文件名
// String fileName = clientId + "_" + System.currentTimeMillis() + ".webm";
// String pathUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileName;
// log.info("文件路径为:{}", pathUrl);
// log.info("文件流的大小为:{}",fullVoiceData.length);
// saveAsWebM(fullVoiceData,pathUrl);
// //开始转换
// WhisperClient whisperClient = SpringUtils.getBean(WhisperClient.class);
// String cacheResultText = whisperClient.handleVoiceToText(pathUrl);
//发送消息
WebSocket webSocket = cacheWebSocket.get(clientId);
if(webSocket != null){
}
webSocket.send("{\"type\": \"input_audio_buffer.commit\"}");
webSocket.send("{\"type\": \"response.create\"}");
// if(webSocket != null){
// webSocket.close(1000,null);
// }
//语音结束,开始进行回答解析
String cacheResultText = cacheClientTts.get(clientId);
log.info("返回的结果为:{}",cacheResultText);
@@ -247,30 +230,14 @@ public class ChatWebSocketHandler {
}
// 接收二进制消息(流数据)
// @OnMessage
// public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) {
// log.info("1、开始接收数据流时间:{}",System.currentTimeMillis()/1000);
// log.info("客户端ID为:{}", clientId);
// // 处理二进制流数据
// byte[] bytes = new byte[byteBuffer.remaining()];
// //从缓冲区中读取数据并存储到指定的字节数组中
// byteBuffer.get(bytes);
//
// // 1. 获取当前会话的缓存
// List<byte[]> fragments = fragmentCache.get(clientId);
// if (fragments == null) {
// fragments = new ArrayList<>();
// fragmentCache.put(clientId, fragments);
// }
// fragments.add(bytes);
// fragmentCache.put(clientId, fragments);
// }
// 连接关闭时调用
@OnClose
public void onClose(Session session, CloseReason reason) {
System.out.println("WebSocket连接已关闭: " + session.getId() + ", 原因: " + reason.getReasonPhrase());
// WebSocket webSocket = cacheWebSocket.get(clientId);
// if(webSocket != null){
// webSocket.close(1000,null);
// }
}
// 发生错误时调用
@@ -293,17 +260,10 @@ public class ChatWebSocketHandler {
System.err.println("字节数组为空无法生成WebM文件");
return false;
}
if (filePath == null || filePath.trim().isEmpty()) {
System.err.println("文件路径不能为空");
return false;
}
// 确保文件以.webm结尾
// if (!filePath.toLowerCase().endsWith(".webm")) {
// filePath += ".webm";
// }
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
@@ -349,7 +309,6 @@ public class ChatWebSocketHandler {
private void createWhisperRealtimeSocket(String clientId){
try{
OkHttpClient client = new OkHttpClient();
// CountDownLatch latch = new CountDownLatch(1);
// 设置 WebSocket 请求
Request request = new Request.Builder()
.url(API_URL)
@@ -419,33 +378,11 @@ public class ChatWebSocketHandler {
// latch.countDown();
}
});
// 等待 WebSocket 关闭
// latch.await();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 合并分片数组为完整字节数组
*/
private byte[] mergeFragments(List<byte[]> fragments) {
// 计算总长度
int totalLength = 0;
for (byte[] fragment : fragments) {
totalLength += fragment.length;
}
// 拼接所有分片
byte[] result = new byte[totalLength];
int offset = 0;
for (byte[] fragment : fragments) {
System.arraycopy(fragment, 0, result, offset, fragment.length);
offset += fragment.length;
}
return result;
}
/**
* 语音流文件格式转换
* @param pathUrl