feat: socket error handling

* create a new session when failed to connect to socket
* reset termnal on connect
This commit is contained in:
wancat authored and Annie Huang committed 2023-11-12 21:23:12 +08:00
1 parent 2639c0ddda
commit ed14491cfc
4 files changed
+39 -31

No files matched your search

+10 -4
View File
@@ -48,13 +48,14 @@ class SessionManager {
return this.session.value return this.session.value
} }
setSession(session) { async setSession(session) {
this.session.value = session this.session.value = session
this.graph.value = new FileGraph(session.graph) this.graph.value = new FileGraph(session.graph)
this.hints.value = session.hints this.hints.value = session.hints
this.status.value = session.status this.status.value = session.status
this.term.clear() this.term.reset()
this.socket.connect(session) await this.socket.connect(session)
this.term.focus()
} }
handleGraphUpdate(event) { handleGraphUpdate(event) {
@@ -92,7 +93,12 @@ class SessionManager {
}) })
const session = res.data const session = res.data
console.log(session) console.log(session)
this.setSession(session) try {
await this.setSession(session)
} catch (err) {
console.log(err)
this.createSession().catch(console.error)
}
} }
} }
+20 -22
View File
@@ -7,30 +7,28 @@ class SocketWrapper {
} }
connect(session) { connect(session) {
if (this.socket) { return new Promise((resolve, reject) => {
this.socket.off() if (this.socket) {
this.socket.disconnect() this.socket.off()
} this.socket.disconnect()
}
this.socket = io('', { this.socket = io('', {
query: { query: {
session_id: session._id, session_id: session._id,
}, },
})
this.socket.on('connect', () => {
console.log('Socket connected to session', session._id)
})
this.socket.once('terminal', () => {
resolve()
})
this.socket.once('error', reject)
this.bindListeners()
}) })
this.socket.on('connect', () => {
console.log('Socket connected to session', session._id)
})
this.socket.on('disconnect', () => {
console.log('Socket disconnected from session', session._id)
})
this.socket.on('error', (err) => {
console.log('Socket error', err)
})
this.bindListeners()
} }
on(event, callback) { on(event, callback) {
+6 -2
View File
@@ -33,6 +33,10 @@ class SocketTerminal {
this.term.onData(callback) this.term.onData(callback)
} }
focus() {
this.term.focus()
}
resizeScreen() { resizeScreen() {
this.fitAddon.fit() this.fitAddon.fit()
} }
@@ -43,8 +47,8 @@ class SocketTerminal {
this.resizeScreen() this.resizeScreen()
} }
clear() { reset() {
this.term.clear() this.term.reset()
} }
} }
+3 -3
View File
@@ -35,7 +35,7 @@ async function connectClient(socket) {
} }
const sessionId = socket.handshake.query.session_id const sessionId = socket.handshake.query.session_id
if (!sessionId) { if (!sessionId) {
socket.send('Session ID not found.') socket.emit('error', 'Session ID not found.')
socket.disconnect() socket.disconnect()
return return
} }
@@ -46,7 +46,7 @@ async function connectClient(socket) {
status: 'active', status: 'active',
}) })
if (!session) { if (!session) {
socket.send('Session not found.') socket.emit('error', 'Session not found.')
socket.disconnect() socket.disconnect()
return return
} }
@@ -61,7 +61,7 @@ async function connectClient(socket) {
container = await getAndStartContainer(session.containerId) container = await getAndStartContainer(session.containerId)
stream = await attachContainer(container, { token }) stream = await attachContainer(container, { token })
} catch (err) { } catch (err) {
socket.send(err.message) socket.emit('error', 'Failed to start container.')
socket.disconnect() socket.disconnect()
return return
} }