mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
34 lines
824 B
JavaScript
34 lines
824 B
JavaScript
import express from 'express'
|
|
import http from 'http'
|
|
import { Server } from 'socket.io'
|
|
import { createContainer, attachContainer } from './docker.js'
|
|
|
|
const app = express()
|
|
const server = http.createServer(app)
|
|
const io = new Server(server)
|
|
|
|
io.on('connection', async (socket) => {
|
|
console.log('Connected to the client.')
|
|
const container = await createContainer()
|
|
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!')
|
|
})
|
|
|
|
server.listen(8080, () => {
|
|
console.log(`Server listening at http://localhost:8080`)
|
|
})
|