mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
28 lines
708 B
JavaScript
28 lines
708 B
JavaScript
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!')
|
|
})
|
|
}
|