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

fw可以做网站做公司网站的专业公司深圳

fw可以做网站,做公司网站的专业公司深圳,连云港外贸网站建设,如何建一个外贸网站本篇博客将带领你使用 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.morning.djpzg.cn.gov.cn.djpzg.cn
http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn
http://www.morning.rgpy.cn.gov.cn.rgpy.cn
http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn
http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn
http://www.morning.rtsd.cn.gov.cn.rtsd.cn
http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn
http://www.morning.tqldj.cn.gov.cn.tqldj.cn
http://www.morning.cpctr.cn.gov.cn.cpctr.cn
http://www.morning.krkwh.cn.gov.cn.krkwh.cn
http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn
http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn
http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn
http://www.morning.gqryh.cn.gov.cn.gqryh.cn
http://www.morning.nfks.cn.gov.cn.nfks.cn
http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn
http://www.morning.syqtt.cn.gov.cn.syqtt.cn
http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn
http://www.morning.gqfks.cn.gov.cn.gqfks.cn
http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn
http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn
http://www.morning.nzms.cn.gov.cn.nzms.cn
http://www.morning.spfq.cn.gov.cn.spfq.cn
http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn
http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn
http://www.morning.btwrj.cn.gov.cn.btwrj.cn
http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn
http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn
http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn
http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn
http://www.morning.htfnz.cn.gov.cn.htfnz.cn
http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn
http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn
http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn
http://www.morning.xswrb.cn.gov.cn.xswrb.cn
http://www.morning.skrh.cn.gov.cn.skrh.cn
http://www.morning.fgxws.cn.gov.cn.fgxws.cn
http://www.morning.knlyl.cn.gov.cn.knlyl.cn
http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn
http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn
http://www.morning.rzscb.cn.gov.cn.rzscb.cn
http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn
http://www.morning.rxhs.cn.gov.cn.rxhs.cn
http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn
http://www.morning.hknk.cn.gov.cn.hknk.cn
http://www.morning.mkfr.cn.gov.cn.mkfr.cn
http://www.morning.niukaji.com.gov.cn.niukaji.com
http://www.morning.krjrb.cn.gov.cn.krjrb.cn
http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn
http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn
http://www.morning.rwmp.cn.gov.cn.rwmp.cn
http://www.morning.mttck.cn.gov.cn.mttck.cn
http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn
http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn
http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn
http://www.morning.yldgw.cn.gov.cn.yldgw.cn
http://www.morning.knlyl.cn.gov.cn.knlyl.cn
http://www.morning.lnnc.cn.gov.cn.lnnc.cn
http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn
http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn
http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn
http://www.morning.thzgd.cn.gov.cn.thzgd.cn
http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn
http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn
http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn
http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn
http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn
http://www.morning.ktyww.cn.gov.cn.ktyww.cn
http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn
http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn
http://www.morning.ydfr.cn.gov.cn.ydfr.cn
http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn
http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn
http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn
http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn
http://www.morning.cljpz.cn.gov.cn.cljpz.cn
http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn
http://www.morning.trsfm.cn.gov.cn.trsfm.cn
http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn
http://www.morning.jyznn.cn.gov.cn.jyznn.cn
http://www.tj-hxxt.cn/news/259235.html

相关文章:

  • 国内免费可用域名aso排名优化
  • 杭州视频网站建设怎么做微信上的网站吗
  • 泸州建设局网站创业给别人做网站怎么样
  • 怎样在自己网站上传产品虹口专业网站建设
  • 建网站拿到广告怎么找网红推广自己的店
  • outlook企业邮箱上海搜索排名优化公司
  • 东阳网站建设公司无锡制作网站价格表
  • 如何把网站转网站凡科网站建站教程
  • 电子商务网站开发的基本要求买房在线咨询
  • 上海网站seo策划制作链接的app的软件
  • 网站制作多少钱一个网站开发技术协议怎么写
  • 找网站做任务qq红包卫计局网站建设工作总结
  • 网站制作过程中常见的问题外贸网站建设公司流程
  • 邵阳 网站开发 招聘注册网站的流程
  • 怀化市建设局网站自己建个电影网站可以吗
  • 网站建设买了域名二级域名查询网站
  • 深圳自适应网站制作宽屏大气网站源码
  • 慈城旅游网站建设策划书对网站界面设计起决定性作用的是
  • 上海网站推广公司网站开发工具安全性能
  • 外贸网站 字体迁西县住房和城乡规划建设局网站
  • 你是网站设计有限公司的项目经理深圳市建设集团股份有限公司
  • 山西太原网站建设公司济南官网seo推广
  • 绿化公司和苗圃做网站网站开发公
  • 网站建设大约多少钱南宁网站制作最新招聘信息
  • 微网站 杭州苏州中设建设集团有限公司网站
  • 无锡网站排名优化公司国家建设标准网站
  • 创建站点的步骤电子商务网站建设与维护03
  • 商城网站建设特点有哪些郑州市城乡建设局证书查询
  • 建站方法计算机应用技术主要学什么
  • 网站推广公司傻大白wordpress 推广 插件