Re-organize code

This commit is contained in:
wancat committed 2023-07-18 20:33:08 +08:00
1 parent 8901b385ad
commit d51a75e5bc
7 files changed
+7 -5

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import { Server } from 'socket.io'
import { getOrCreateContainer, attachContainer } from '../utils/docker.js'
export default (server) => {
const io = new Server(server)
io.on('connection', async (socket) => {
console.log('Connected to the client.')
const container = await getOrCreateContainer('odyssey-test')
const stream = await attachContainer(container)
stream.on('data', (chunk) => {
socket.send(chunk)
})
socket.on('message', function incoming(message) {
console.log(`From client: ${message}`)
stream.write(message)
})
socket.on('close', () => {
console.log('Disconnected from the client.')
})
socket.send('Hello from server!')
})
}