socket.js
4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-02-25 10:30:47
* @LastEditors: 前端 qianduan01@didihu.com.cn
* @LastEditTime: 2024-03-06 10:02:16
* @FilePath: \dj-scrm-wx-work-attachment-psd\src\mixins\socket.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
const Q_NUM = 9
import { ROLE_LIST, AI_TYPE } from '@/utils/constants'
export default {
data() {
return {
path: window.$fetchConfig.baseURL,
isShow: true,
isSend: true,
timer_chat: null,
timer_jump: null,
messageAll: [],
issue: '',
contentList: [],
deleteArr: [],
socket: ''
}
},
mounted() {
// 初始化
this.init()
},
methods: {
init: function () {
this.socket = new WebSocket(this.path)
// 监听socket连接
this.socket.onopen = this.open
// 监听socket错误信息
this.socket.onerror = this.error
// 监听socket消息
this.socket.onmessage = this.getMessage
this.socket.onclose = this.close
},
open: function () {
console.log('socket连接成功')
if (this.defaultFun) {
this.defaultFun()
}
},
error: function () {
console.log('连接错误')
setTimeout(this.init, 1000)
},
getMessage: function (res) {
this.isShow = false
let msg = res.data
if (this.summarize) {
this.summarize(msg)
return
}
if (msg == '<rdfast stop>') {
if (!this.timer_tip)
this.timer_tip = setInterval(() => {
if (this.type == AI_TYPE.sparring && this.messageAll.length == 0) {
this.getIssuesList()
}
if (this.messageAll.length == 0) {
clearInterval(this.timer_tip)
this.timer_tip = null
}
}, 100)
if (this.type != AI_TYPE.sparring_reply) {
this.isSend = true
return
}
}
if (this.type != AI_TYPE.sparring_reply) {
if (this.contentList.length == 1) {
this.contentList.push({
contentType: 1,
content: ''
})
}
this.messageAll.push(msg)
if (!this.timer_chat)
this.timer_chat = setInterval(() => {
const len = this.contentList.length - 1
const i = this.messageAll.shift()
if (this.messageAll.length == 0) {
clearInterval(this.timer_chat)
this.timer_chat = null
}
this.contentList[len].content += i
}, 50)
this.toBottom()
} else {
this.setTips(msg)
}
},
// 发送消息给被连接的服务端
/**
* type 有三个值:
summarize 表示总结
sparring 表示对练
chat 表示问答
*/
sendMessage: function (params) {
if (this.socket.readyState == WebSocket.OPEN) {
this.socket.send(params)
} else {
this.init()
}
},
pushMsg(type, history, question_d = '') {
if (type != AI_TYPE.sparring_reply)
this.contentList.push({
contentType: 2,
content: this.issue
})
let question = this.issue
this.type = type
if (question_d) {
for (let i = 0; i < 2; i++) {
this.sendMessage(
JSON.stringify({
type,
question: question_d ? question_d : question,
history: JSON.stringify([i % 2 == 0 ? { role: ROLE_LIST.system, content: '以女性的口吻回复' } : { role: ROLE_LIST.system, content: '以男性的口吻回复' }, ...history])
})
)
}
} else {
this.sendMessage(
JSON.stringify({
type,
question: question_d ? question_d : question,
history: JSON.stringify(history)
})
)
}
if (type != AI_TYPE.sparring_reply)
this.$nextTick(() => {
this.contentList.push({
contentType: 1,
content: ''
})
})
this.toBottom()
this.isSend = false
this.issue = ''
},
close() {
this.socket.close() // 关闭 websocket
},
toBottom() {
this.$nextTick(() => {
this.$refs.contentView.scrollTop = 999999999
})
}
},
beforeDestroy() {
// 销毁监听
this.close()
clearInterval(this.timer_jump)
}
}