java springboot 中使用webSocket接入openAI接口調(diào)用chatGPT3.5接口實(shí)現(xiàn)自由返回
在springboot中添加webSocketServer
@Component
@Anonymous
@ServerEndpoint(“/websocket/{id}”) // 訪問路徑: ws://localhost:8080/websocket
public class WebSocketServer {
文章來源:http://www.zghlxwxcb.cn/news/detail-621735.html
protected static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
/**
* 客戶端ID
*/
private String id="";
/**
* 與某個客戶端的連接會話,需要通過它來給客戶端發(fā)送數(shù)據(jù)
*/
private Session session;
/**
* 記錄當(dāng)前在線連接數(shù)(為保證線程安全,須對使用此變量的方法加lock或synchronized)
*/
private static int onlineCount = 0;
/**
* 用來存儲當(dāng)前在線的客戶端(此map線程安全)
*/
private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, StringBuffer> stringBufferMap = new ConcurrentHashMap<>();
/**
* 連接建立成功后調(diào)用
*/
@OnOpen
public void onOpen(@PathParam(value = "id") String id, Session session) {
this.session = session;
// 接收到發(fā)送消息的客戶端編號
this.id = id;
// 加入map中
StringBuffer stringBuffer = new StringBuffer();
stringBufferMap.put(id,stringBuffer);
webSocketMap.put(id, this);
// try {
// sendMessage(“WebSocket連接成功”);
// } catch (Exception e) {
//
// }
}
/**
* 發(fā)送消息
* @param message 要發(fā)送的消息
*/
private void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}文章來源地址http://www.zghlxwxcb.cn/news/detail-621735.html
// 關(guān)閉連接調(diào)用
@OnClose
public void close() {
}
// 接收消息
@OnMessage
public void message(String message, Session session) {
System.out.println("client send: " + messag
到了這里,關(guān)于java springboot 整合webSocket接入調(diào)用chatGPT3.5接口實(shí)現(xiàn)自由返回的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!