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

一线城市做网站工资有多少seo是什么及作用

一线城市做网站工资有多少,seo是什么及作用,全国最新实时疫情,网站开发测试方法本节将提供基于 Spring WebFlux 和 SSE 实现类ChatGPT流式回复效果的完整代码示例&#xff0c;并详细说明所需的依赖和配置。 1. 项目配置 构建工具: Maven 或 Gradle依赖: <dependency><groupId>org.springframework.boot</groupId><artifactId>sp…

本节将提供基于 Spring WebFlux 和 SSE 实现类ChatGPT流式回复效果的完整代码示例,并详细说明所需的依赖和配置。

1. 项目配置

  • 构建工具: Maven 或 Gradle
  • 依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

2. 后端代码 (Spring WebFlux)

package com.example.ssedemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import org.springframework.http.codec.ServerSentEvent;import java.time.Duration;@SpringBootApplication
@RestController
public class SseDemoApplication {public static void main(String[] args) {SpringApplication.run(SseDemoApplication.class, args);}@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<ServerSentEvent<String>> streamChatGPTReply(@RequestParam String message) {// 模拟调用 ChatGPT API 获取回复String reply = "收到消息: " + message + ". 正在思考...";return Flux.<String>create(sink -> {sink.next(reply); // 先发送初始回复// 模拟逐字生成回复for (int i = 0; i < reply.length(); i++) {try {Thread.sleep(100); // 模拟延迟sink.next(reply.substring(0, i + 1));} catch (InterruptedException e) {throw new RuntimeException(e);}}sink.complete();}).map(data -> ServerSentEvent.<String>builder().data(data).build()).delayElements(Duration.ofMillis(100)); // 每隔一段时间发送一个字符}
}

 另外一种方式:

private final WebClient webClient; // 用于调用外部 APIpublic ChatController(WebClient.Builder webClientBuilder) {this.webClient = webClientBuilder.baseUrl("http://api.example.com").build(); 
}@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<String>> streamChatGPTReply(@RequestParam String message) {// 使用 WebClient 异步调用外部 APIreturn webClient.post().uri("/api/external") .bodyValue(message).retrieve().bodyToFlux(String.class) // 假设 API 返回 String 类型数据.map(data -> ServerSentEvent.<String>builder().data(data) // 将 API 响应数据包装到 SSE 事件中.build()).delayElements(Duration.ofMillis(100));
}

3. 前端代码 (HTML & JavaScript)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>SSE Demo</title>
</head>
<body><h1>SSE Chat Demo</h1><div id="output"></div><input type="text" id="message"><button onclick="sendMessage()">发送</button><script>const output = document.getElementById('output');function sendMessage() {const message = document.getElementById('message').value;const eventSource = new EventSource('/stream?message=' + message);eventSource.onmessage = (event) => {output.innerHTML += event.data + '<br>';};eventSource.onerror = (error) => {console.error('SSE 连接错误:', error);eventSource.close();};}</script>
</body>
</html>

4. 代码解析

  • 后端:
    • @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE): 指定响应类型为 text/event-stream,这是 SSE 的标准 MIME 类型。
    • Flux<ServerSentEvent<String>>: 使用 Spring WebFlux 的 Flux 类型返回数据流,并使用 ServerSentEvent 包装每个数据项.
    • sink.next(): 向数据流中发送数据。
    • sink.complete(): 通知数据流结束。
  • 前端:
    • new EventSource('/stream'): 创建 EventSource 对象,连接到后端 SSE 接口.
    • eventSource.onmessage: 监听 message 事件,接收后端推送的数据.
    • eventSource.onerror: 监听连接错误.

5. 运行 & 测试

  1. 启动 Spring Boot 应用.
  2. 访问 http://localhost:8080 (默认端口).
  3. 在输入框中输入消息并点击发送,观察逐字显示的效果.

总结

本文详细介绍了如何使用 Spring WebFlux 和 SSE 实现类似 ChatGPT 的流式回复效果,并提供了完整的代码示例。希望读者能够通过本文掌握该技术,并在实际项目中灵活运用。

http://www.bjxfkj.com.cn/article/105009.html

相关文章:

  • 网站排名提高网页制作代码
  • 现在网站一般做多大的在线制作网页网站
  • 制作网站民治免费推广网站2024
  • python 做网站 案例网站权重一般有几个等级
  • 宁波高等级公路建设指挥部网站游戏推广员是做什么的
  • 关于做网站策划书网站建设的推广渠道
  • 星锐网站建设谷歌sem服务商
  • 手机网站开发用什么框架好郑州seo管理
  • 平度网站制作搜索引擎优化免费
  • 中工互联网站建设网络推广法
  • 泗县做网站小红书怎么推广引流
  • 做外贸用什么网站好手机优化大师下载2022
  • 专业做包包的网站seo关键词优化软件手机
  • 曰本做爰视频网站今日新闻最新事件
  • wordpress注入点阜阳seo
  • 如何鉴定网站做的好坏长春网站优化
  • 新网站建设流程图如何在百度做免费推广产品
  • 淄博企业高端网站建设网络宣传策划方案
  • 南昌网站建设服务器seo网站推广目的
  • 网站编辑怎么做内容分类优化措施最新回应
  • wordpress模板破解优质的seo网站排名优化软件
  • 完善网站建设工作北京seo代理商
  • 做环评在发改委网站申请产品设计公司
  • 网页制作教程素材五行seo博客
  • 专门做离异相亲的网站安康地seo
  • 如何做盗版小说网站凡科建站代理登录
  • c 网站开发实例企业微信营销系统
  • 写作网站可保存个人网页制作
  • 东莞百度seo地址搜索引擎优化的主要特征
  • 购物商城网站建设流程常州网站建设制作