mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Move server back to index.js
This commit is contained in:
1 parent
de637f3cf6
commit
45a0aa6249
5 files changed
+38
-38
No files matched your search
@@ -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!')
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user