Move server back to index.js

This commit is contained in:
wancat committed 2023-07-15 15:43:13 +08:00
1 parent de637f3cf6
commit 45a0aa6249
5 files changed
+38 -38

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import { Server } from 'socket.io'
import { getOrCreateContainer, attachContainer } from './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!')
})
}