mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Success to execute and show output using Websocket
This commit is contained in:
1 parent
f17c2a9ef5
commit
69ef435b77
3 files changed
+19
-21
No files matched your search
+1
-1
@@ -14,7 +14,7 @@ socket.on('open', function open() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
socket.on('message', function incoming(data) {
|
socket.on('message', function incoming(data) {
|
||||||
console.log(`From server: ${data}`)
|
console.log(`${data}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('close', function close() {
|
socket.on('close', function close() {
|
||||||
|
|||||||
+9
-19
@@ -13,10 +13,11 @@ const containerOptions = {
|
|||||||
StdinOnce: false,
|
StdinOnce: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createContainer() {
|
export async function createContainer() {
|
||||||
try {
|
try {
|
||||||
const container = await engine.createContainer(containerOptions)
|
const container = await engine.createContainer(containerOptions)
|
||||||
await container.start()
|
await container.start()
|
||||||
|
console.log(container.id)
|
||||||
return container
|
return container
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
@@ -24,23 +25,12 @@ async function createContainer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function attachContainer() {
|
export async function attachContainer(container) {
|
||||||
const container = await createContainer()
|
|
||||||
console.log(container.id)
|
|
||||||
// Create an exec instance with bash shell
|
// Create an exec instance with bash shell
|
||||||
|
return container.attach({
|
||||||
container.attach(
|
stream: true,
|
||||||
{
|
stdin: true,
|
||||||
stream: true,
|
stdout: true,
|
||||||
stdin: true,
|
stderr: true,
|
||||||
stdout: true,
|
})
|
||||||
stderr: true,
|
|
||||||
ws: true,
|
|
||||||
},
|
|
||||||
(err, stream) => {
|
|
||||||
stream.pipe(process.stdout)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
attachContainer()
|
|
||||||
+9
-1
@@ -1,16 +1,24 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import http from 'http'
|
import http from 'http'
|
||||||
import { Server } from 'socket.io'
|
import { Server } from 'socket.io'
|
||||||
|
import { createContainer, attachContainer } from './docker.js'
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const server = http.createServer(app)
|
const server = http.createServer(app)
|
||||||
const io = new Server(server)
|
const io = new Server(server)
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', async (socket) => {
|
||||||
console.log('Connected to the client.')
|
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) {
|
socket.on('message', function incoming(message) {
|
||||||
console.log(`From client: ${message}`)
|
console.log(`From client: ${message}`)
|
||||||
|
stream.write(message + '\n')
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('close', () => {
|
socket.on('close', () => {
|
||||||
|
|||||||
Reference in new issue
Block a user