1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-23 20:12:08 +03:00

feat(web app): wait for TCP client to be connected first

This commit is contained in:
louistiti 2022-03-21 22:48:52 +08:00
parent 95bfcfe422
commit bc228a6860
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
5 changed files with 22 additions and 4 deletions

View File

@ -28,13 +28,17 @@ export default class Client {
return this._recorder
}
init () {
init (loader) {
this.chatbot.init()
this.socket.on('connect', () => {
this.socket.emit('init', this.client)
})
this.socket.on('ready', () => {
loader.stop()
})
this.socket.on('answer', (data) => {
this.chatbot.receivedFrom('leon', data)
})

View File

@ -40,7 +40,7 @@ document.addEventListener('DOMContentLoaded', () => {
}
logger.innerHTML += sLogger
client.init()
client.init(loader)
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
@ -96,8 +96,6 @@ document.addEventListener('DOMContentLoaded', () => {
console.error('MediaDevices.getUserMedia() is not supported on your browser.')
}
loader.stop()
document.addEventListener('keydown', (e) => {
onkeydowndocument(e, () => {
if (rec.enabled === false) {

View File

@ -136,6 +136,15 @@ server.handleOnConnection = (socket) => {
log.info(`Type: ${data}`)
log.info(`Socket id: ${socket.id}`)
// Check whether the TCP client is connected to the TCP server
if (global.tcpClient.isConnected) {
socket.emit('ready')
} else {
global.tcpClient.ee.on('connected', () => {
socket.emit('ready')
})
}
if (data === 'hotword-node') {
// Hotword triggered
socket.on('hotword-detected', (data) => {

View File

@ -54,6 +54,7 @@ class Nlu {
log.success('NLP model loaded')
this.ner = new Ner(this.nlp.ner)
resolve()
} catch (err) {
this.brain.talk(`${this.brain.wernicke('random_errors')}! ${this.brain.wernicke('errors', 'nlu', { '%error%': err.message })}.`)

View File

@ -14,6 +14,7 @@ export default class TcpClient {
this.tcpSocket = new Net.Socket()
this._ee = new EventEmitter()
this._status = this.tcpSocket.readyState
this._isConnected = false
log.title('TCP Client')
log.success('New instance')
@ -22,6 +23,7 @@ export default class TcpClient {
log.title('TCP Client')
log.success(`Connected to the TCP server tcp://${this.host}:${this.port}`)
this._isConnected = true
this._ee.emit('connected', null)
})
@ -74,6 +76,10 @@ export default class TcpClient {
return this._ee
}
get isConnected () {
return this._isConnected
}
emit (topic, data) {
const obj = {
topic,