当前位置: 首页 > news >正文

wordpress页面模板位置优化大师软件大全

wordpress页面模板位置,优化大师软件大全,单页网站建设一般收费,中国移动app免费下载本篇博客将带领你使用 Spring Boot、WebSocket 和 JavaScript 实现一个类似 ChatGPT 的流式回复效果。前端发送消息后#xff0c;后端接收消息并请求 AI API#xff0c;并将 AI 返回的流式响应实时推送到前端#xff0c;最终在聊天界面呈现出逐字出现的打字效果。 技术原理…本篇博客将带领你使用 Spring Boot、WebSocket 和 JavaScript 实现一个类似 ChatGPT 的流式回复效果。前端发送消息后后端接收消息并请求 AI API并将 AI 返回的流式响应实时推送到前端最终在聊天界面呈现出逐字出现的打字效果。 技术原理 WebSocket 全双工通信: WebSocket 协议提供全双工通信通道允许服务器和客户端之间进行双向实时数据传输非常适合实现流式数据传输。 Spring WebFlux 响应式编程: Spring WebFlux 基于 Reactor 库支持响应式流处理可以处理 AI API 返回的流式数据。 SSEServer-Sent Events SSE 是一种基于 HTTP 的单向服务器推送技术可以将 AI 返回的流式数据实时推送到前端。 实现步骤 一、后端实现 (Spring Boot) 添加依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId /dependency dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId /dependency创建 WebSocket 处理器 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux;import java.time.Duration;Controller public class ChatController {Autowiredprivate SimpMessagingTemplate messagingTemplate;Autowiredprivate WebClient webClient; // 用于调用 AI APIMessageMapping(/chat.sendMessage) // 接收来自客户端的消息public void sendMessage(Payload Message message) throws Exception {// 构造 AI API 请求ApiRequest apiRequest new ApiRequest(message.getContent()); // 假设 AI API 接受 ApiRequest 对象// 调用 AI API获取流式响应FluxString apiResponse callAiApi(apiRequest);// 处理 AI API 响应并逐条发送给前端apiResponse.delayElements(Duration.ofMillis(50)) // 模拟打字延迟.subscribe(chunk - {Message responseMessage new Message(AI, chunk);messagingTemplate.convertAndSend(/topic/chat, responseMessage);});}// 调用 AI APIprivate FluxString callAiApi(ApiRequest request) {// 将 ApiRequest 对象转换为 JSON 字符串// ...// 发送 POST 请求并指定返回数据类型为 FluxStringreturn webClient.post().uri(https://api.example.com/ai-api) // 替换为实际的 AI API 地址.bodyValue(request).retrieve().bodyToFlux(String.class);}// 消息类public static class Message {private String sender;private String content;// 构造函数getter 和 setter}// AI API 请求类public static class ApiRequest {private String prompt;// 构造函数getter 和 setter} }WebSocket 配置 import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;Configuration EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.enableSimpleBroker(/topic);config.setApplicationDestinationPrefixes(/app);}Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint(/ws).withSockJS();} }二、前端实现 (JavaScript) !DOCTYPE html html headtitleChat Application/titlescript srchttps://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.5.1/sockjs.min.js/scriptscript srchttps://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js/script /head bodydiv idchat-containerdiv idchat-output/divinput typetext idmessage-input placeholderEnter message...button onclicksendMessage()Send/button/divscript// 连接 WebSocketvar socket new SockJS(/ws);var stompClient Stomp.over(socket);stompClient.connect({}, function(frame) {console.log(Connected: frame);// 订阅聊天频道stompClient.subscribe(/topic/chat, function(message) {showMessage(JSON.parse(message.body));});});// 发送消息function sendMessage() {var message document.getElementById(message-input).value;stompClient.send(/app/chat.sendMessage, {}, JSON.stringify({sender: user, // 可替换为实际用户名content: message}));}// 显示消息function showMessage(message) {var chatOutput document.getElementById(chat-output);var messageElement document.createElement(div);if (message.sender AI) {// AI 回复逐字显示typeWriter(messageElement, message.content, 0);} else {messageElement.innerHTML strong message.sender :/strong message.content;chatOutput.appendChild(messageElement);}}// 模拟打字效果function typeWriter(element, text, i) {if (i text.length) {element.innerHTML text.charAt(i);setTimeout(function() {typeWriter(element, text, i 1);}, 50); // 调整打字速度} else {document.getElementById(chat-output).appendChild(element);}}/script /body /html总结 本文介绍了如何使用 Spring Boot 和 WebSocket 实现类似 ChatGPT 的流式回复效果并详细讲解了每个步骤的技术原理和代码实现。通过这种方式我们可以构建更加 engaging 和 interactive 的 Web 应用为用户带来全新的体验。
http://www.hkea.cn/news/14532045/

相关文章:

  • 网站建设会议记录如何改变网站首页栏目
  • 企业网站建设成本微信开放平台小程序
  • 网站备案证书在哪里下载潍坊seo网站推广
  • 衡水网站制作费用网站兼容所有浏览器
  • 手机网站设计公司只找亿企邦大良营销网站建设机构
  • 济南网站优化推广公司vs2017移动网站开发
  • 网站服务器 内存图书馆网站建设的意义
  • 分析某个网站建设长春市网站制作
  • 网站建立网络优化泉州最专业手机网站建设哪家好
  • 海外注册域名的网站好网站分析工具有哪些
  • 淘客网站如何做个人养老金
  • 魔方 网站苏州网站制作开发公司
  • 网站建设珠江摩尔王占山事迹
  • 吉林平台网站建设多少钱用jsp做网站的难点
  • 电子商务网站建设管理答案网站开通告知书
  • 网站开发有哪几种语言国外ps网站
  • 正规网站建设咨询电话wordpress个人展示
  • 新手做网站教程青岛建设集团建兴工程有限公司
  • 网站建设是怎么赚钱营销网站的成功案例
  • 网站的视频做gifwordpress 做值班表
  • 黑龙江建设网站网页网页设计班
  • 丹阳网站设计建设银行河北招聘网站
  • 如何做公司网站wordpress ftp设置
  • 个人备案网站做淘宝客可以用吗企业网站建设申请怎么写
  • 请问下网站开发怎么弄没有icp备案的ip地址访问的网站会被各搜索引擎收录吗
  • 兼职网站高中生在家可做传奇怎么做网站
  • 网站开发手册网站模块功能
  • php wordpress 配置成都排名seo公司
  • 内网建站教程wordpress会员等级
  • 东莞网站公司建设网站中国防疫政策马上要变化了