|
|
|
@ -15,40 +15,37 @@
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
<div
|
|
|
|
class="wordbox"
|
|
|
|
class="wordbox"
|
|
|
|
ref="containerRef"
|
|
|
|
ref="contentRef"
|
|
|
|
|
|
|
|
@scroll="contentScrollFN"
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
<div
|
|
|
|
id="content"
|
|
|
|
id="content"
|
|
|
|
v-html="content"
|
|
|
|
v-html="content"
|
|
|
|
></span>
|
|
|
|
></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</base-container>
|
|
|
|
</base-container>
|
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
<script setup>
|
|
|
|
import { ref, nextTick } from 'vue';
|
|
|
|
import { ref } from 'vue';
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source';
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source';
|
|
|
|
|
|
|
|
import { useAutoScroll } from '@/hooks/useAutoScroll';
|
|
|
|
|
|
|
|
const { canAutoScroll, contentRef, contentScrollFN } = useAutoScroll();
|
|
|
|
|
|
|
|
|
|
|
|
const content = ref('');
|
|
|
|
const content = ref('');
|
|
|
|
const controller = ref(null);
|
|
|
|
const controller = ref(null);
|
|
|
|
|
|
|
|
|
|
|
|
const updateContent = (event) => {
|
|
|
|
const updateContent = (event) => {
|
|
|
|
console.log(event);
|
|
|
|
content.value += event.data;
|
|
|
|
console.log('sse收到消息');
|
|
|
|
|
|
|
|
content.value += event.data + '<br/>';
|
|
|
|
|
|
|
|
console.log(content.value, 'content内容');
|
|
|
|
|
|
|
|
if (event.data === 'End') {
|
|
|
|
if (event.data === 'End') {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 注释部分为前端手动关闭sse连接的实现
|
|
|
|
* 注释部分为前端手动关闭sse连接的实现
|
|
|
|
* 这里不受冻关闭原因是@microsoft/fetch-event-source这个框架可以正确检测后段服务端是否关闭了连接,所以不需要手动关闭
|
|
|
|
* 这里不手动关闭原因是@microsoft/fetch-event-source这个框架可以正确检测后段服务端是否关闭了连接,所以不需要手动关闭
|
|
|
|
* 浏览器原生new EventSource()方法无法检测后端服务端是否关闭连接,所以需要手动关闭(每次后段关闭都会进onerror事件)
|
|
|
|
* 浏览器原生new EventSource()方法无法检测后端服务端是否关闭连接,所以需要手动关闭(每次后段关闭都会进onerror事件)
|
|
|
|
* 具体请看另外一个demo sseNative.vue
|
|
|
|
* 具体请看另外一个demo sseNative.vue
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
// handleClose();
|
|
|
|
// handleClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleError = (event) => {
|
|
|
|
const handleError = (event) => {
|
|
|
|
@ -58,19 +55,15 @@ const handleError = (event) => {
|
|
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
const handleClose = () => {
|
|
|
|
console.log('sse关闭');
|
|
|
|
console.log('sse关闭');
|
|
|
|
// controller.value.abort();
|
|
|
|
controller.value.abort();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const containerRef = ref(null);
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
|
|
|
containerRef.value.scrollTop = containerRef.value.scrollHeight;
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSend = () => {
|
|
|
|
const handleSend = () => {
|
|
|
|
|
|
|
|
canAutoScroll.value = true;
|
|
|
|
controller.value = new AbortController();
|
|
|
|
controller.value = new AbortController();
|
|
|
|
const signal = controller.value.signal;
|
|
|
|
const signal = controller.value.signal;
|
|
|
|
/** @microsoft/fetch-event-source该框架支持POST方法,并且支持传参 */
|
|
|
|
/** @microsoft/fetch-event-source该框架支持POST方法,并且支持传参 */
|
|
|
|
fetchEventSource('http://localhost:3000/events', {
|
|
|
|
fetchEventSource('http://192.168.197.120:3000/events', {
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
body: JSON.stringify({
|
|
|
|
question: '你好,我是小雅,请问有什么可以帮助您?',
|
|
|
|
question: '你好,我是小雅,请问有什么可以帮助您?',
|
|
|
|
@ -87,11 +80,19 @@ const handleSend = () => {
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.wordbox {
|
|
|
|
.wordbox {
|
|
|
|
width: 100%;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
height: 200px;
|
|
|
|
overflow: auto;
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
|
|
|
overflow-x: hidden;
|
|
|
|
padding: 20px;
|
|
|
|
padding: 20px;
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
margin: 20px 0;
|
|
|
|
margin: 20px 0;
|
|
|
|
border-radius: 5px;
|
|
|
|
border-radius: 5px;
|
|
|
|
|
|
|
|
scrollbar-width: none;
|
|
|
|
|
|
|
|
word-break: break-all;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.wordbox::-webkit-scrollbar {
|
|
|
|
|
|
|
|
display: none; /* 隐藏滚动条 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</style>
|
|
|
|
|