Skip to content

Commit 4c44bc2

Browse files
committed
对话队列
1 parent 8007c4b commit 4c44bc2

File tree

2 files changed

+113
-20
lines changed

2 files changed

+113
-20
lines changed

client/src/views/Chat.vue

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default {
2929
data() {
3030
return {
3131
chat: '',
32+
chatList: [],
3233
chatShow: false,
3334
chatShowTimer: null,
3435
socket: null,
@@ -69,23 +70,72 @@ export default {
6970
if (url) {
7071
const speech = new Audio(url)
7172
speech.load()
72-
speech.play()
73+
speech
74+
.play()
75+
.then(() => {})
76+
.catch(() => {
77+
setTimeout(() => {
78+
this.playNextChat()
79+
}, this.chat.length * 300)
80+
})
81+
speech.onended = () => {
82+
this.playNextChat()
83+
}
84+
} else {
85+
setTimeout(() => {
86+
this.playNextChat()
87+
}, this.chat.length * 300)
7388
}
89+
} else {
90+
setTimeout(() => {
91+
this.playNextChat()
92+
}, this.chat.length * 300)
93+
}
94+
},
95+
say(chat, voiceUrl) {
96+
this.chat = chat
97+
this.$nextTick(() => {
98+
this.chatShow = true
99+
this.playVoice(voiceUrl)
100+
})
101+
clearTimeout(this.chatShowTimer)
102+
this.chatShowTimer = setTimeout(() => {
103+
this.chatShow = false
104+
}, this.chat.length * 1000)
105+
},
106+
addChat(chatData) {
107+
let playFlag = false
108+
if (this.chatList.length === 0) {
109+
playFlag = true
110+
}
111+
this.chatList.push(chatData)
112+
if (playFlag) {
113+
this.playNextChat(true)
114+
}
115+
},
116+
removeChat() {
117+
if (this.chatList.length > 0) {
118+
this.chatList.shift()
119+
}
120+
},
121+
playNextChat(noremove) {
122+
if (!noremove) {
123+
this.removeChat()
124+
}
125+
const chatData = this.chatList[0]
126+
if (chatData) {
127+
this.say(chatData.chat, chatData.voiceUrl)
74128
}
75129
},
76130
toSocket() {
77131
this.socket = io.connect('/socketchat')
78132
this.socket.on('msg', (data) => {
133+
const chatData = {
134+
chat: data.message,
135+
voiceUrl: data.voiceUrl,
136+
}
137+
this.addChat(chatData)
79138
console.log(data)
80-
this.chat = data.message
81-
this.$nextTick(() => {
82-
this.chatShow = true
83-
this.playVoice(data.voiceUrl)
84-
})
85-
clearTimeout(this.chatShowTimer)
86-
this.chatShowTimer = setTimeout(() => {
87-
this.chatShow = false
88-
}, this.chat.length * 1000)
89139
})
90140
this.socket.on('getSettingData', (data) => {
91141
console.log(data)

client/src/views/Live2d.vue

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
mode: null,
2727
model: null,
2828
app: null,
29+
chatList: [],
2930
chat: '',
3031
chatShow: false,
3132
chatShowTimer: null,
@@ -137,20 +138,27 @@ export default {
137138
.then(() => {
138139
clearTimeout(this.sleepTimer)
139140
this.model.internalModel.motionManager.stopAllMotions()
140-
console.log(123)
141141
this.$nextTick(() => {
142142
this.mouthOpen = true
143143
})
144144
})
145145
.catch(() => {
146146
this.mouthOpen = false
147147
this.randomMotion()
148+
setTimeout(() => {
149+
this.playNextChat()
150+
}, this.chat.length * 300)
148151
})
149152
150153
speech.onended = () => {
151154
this.mouthOpen = false
152155
this.randomMotion()
156+
this.playNextChat()
153157
}
158+
} else {
159+
setTimeout(() => {
160+
this.playNextChat()
161+
}, this.chat.length * 300)
154162
}
155163
} else {
156164
clearTimeout(this.sleepTimer)
@@ -161,21 +169,56 @@ export default {
161169
this.mouthOpen = false
162170
this.randomMotion()
163171
}, this.chat.length * 500)
172+
setTimeout(() => {
173+
this.playNextChat()
174+
}, this.chat.length * 300)
175+
}
176+
},
177+
say(chat, voiceUrl) {
178+
this.chat = chat
179+
this.$nextTick(() => {
180+
this.chatShow = true
181+
this.playVoice(voiceUrl)
182+
})
183+
clearTimeout(this.chatShowTimer)
184+
this.chatShowTimer = setTimeout(() => {
185+
this.chatShow = false
186+
}, this.chat.length * 1000)
187+
},
188+
addChat(chatData) {
189+
let playFlag = false
190+
if (this.chatList.length === 0) {
191+
playFlag = true
192+
}
193+
this.chatList.push(chatData)
194+
if (playFlag) {
195+
this.playNextChat(true)
196+
}
197+
},
198+
removeChat() {
199+
if (this.chatList.length > 0) {
200+
this.chatList.shift()
201+
}
202+
},
203+
playNextChat(noremove) {
204+
if (!noremove) {
205+
this.removeChat()
206+
}
207+
const chatData = this.chatList[0]
208+
if (chatData) {
209+
this.say(chatData.chat, chatData.voiceUrl)
164210
}
165211
},
166212
toSocket() {
167213
this.socket = io.connect('/socketchat')
168214
this.socket.on('msg', (data) => {
215+
const chatData = {
216+
chat: data.message,
217+
voiceUrl: data.voiceUrl,
218+
}
219+
this.addChat(chatData)
220+
169221
console.log(data)
170-
this.chat = data.message
171-
this.$nextTick(() => {
172-
this.chatShow = true
173-
this.playVoice(data.voiceUrl)
174-
})
175-
clearTimeout(this.chatShowTimer)
176-
this.chatShowTimer = setTimeout(() => {
177-
this.chatShow = false
178-
}, this.chat.length * 1000)
179222
})
180223
this.socket.on('getSettingData', (data) => {
181224
console.log(data)

0 commit comments

Comments
 (0)