package com.guwan.ws;

import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;

@Slf4j
@Component
@ServerEndpoint(value = "/ws/voice")
public class VoiceWebSocketServer {

    @OnOpen
    public void onOpen(Session session) {
        log.info("WebSocket 连接已建立，session ID: {}", session.getId());
    }

    @OnMessage
    public void onMessage(Session session, String audioData) {
        System.out.println("WebSocket 连接已处理 = " + session);
        sendMessage(session, audioData);
    }

    @OnClose
    public void onClose(Session session) {
        log.info("WebSocket 连接已关闭，session ID: {}", session.getId());
    }

    @OnError
    public void onError(Throwable error) {
        log.error("onError", error);
    }

    private void sendMessage(Session session, String text) {
        try {
            session.getBasicRemote().sendText(text);
        } catch (Exception e) {
            log.error("Error sending message", e);
        }
    }

    // 语音识别服务方法（示例）
    private String transcribeAudio() {
        // 模拟的语音识别
        return "识别到的文本内容";
    }
}
