Success to execute and show output using Websocket

This commit is contained in:
wancat committed 2023-07-15 14:36:51 +08:00
1 parent f17c2a9ef5
commit 69ef435b77
3 files changed
+19 -21

No files matched your search

+9 -1
View File
@@ -1,16 +1,24 @@
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', (socket) => {
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.toString())
})
socket.on('message', function incoming(message) {
console.log(`From client: ${message}`)
stream.write(message + '\n')
})
socket.on('close', () => {