Move server back to index.js

This commit is contained in:
wancat committed 2023-07-15 15:43:13 +08:00
1 parent de637f3cf6
commit 45a0aa6249
5 files changed
+38 -38

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@ stdin.setRawMode(true)
stdin.resume() stdin.resume()
stdin.setEncoding('utf8') stdin.setEncoding('utf8')
const socket = io('ws://localhost:8080') const socket = io('ws://localhost:3000')
socket.on('open', function open() { socket.on('open', function open() {
console.log('Connected to the server.') console.log('Connected to the server.')
+4 -2
View File
@@ -16,7 +16,9 @@ const containerOptions = {
export async function getOrCreateContainer(name) { export async function getOrCreateContainer(name) {
try { try {
const container = engine.getContainer(name) const container = engine.getContainer(name)
await container.inspect() if (!(await container.inspect()).State.Running) {
await container.start()
}
return container return container
} catch (error) { } catch (error) {
const container = await engine.createContainer({ const container = await engine.createContainer({
@@ -29,7 +31,7 @@ export async function getOrCreateContainer(name) {
} }
} }
export async function attachContainer(container) { export function attachContainer(container) {
// Create an exec instance with bash shell // Create an exec instance with bash shell
return container.attach({ return container.attach({
stream: true, stream: true,
+6 -2
View File
@@ -1,12 +1,16 @@
import express from 'express' import express from 'express'
import http from 'http'
import socketServer from './socket.js'
const app = express()
const port = 3000 const port = 3000
const app = express()
const server = http.createServer(app)
socketServer(server)
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Hello World!') res.send('Hello World!')
}) })
app.listen(port, () => { server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`) console.log(`Server listening at http://localhost:${port}`)
}) })
+27
View File
@@ -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!')
})
}
-33
View File
@@ -1,33 +0,0 @@
import express from 'express'
import http from 'http'
import { Server } from 'socket.io'
import { getOrCreateContainer, 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 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!')
})
server.listen(8080, () => {
console.log(`Server listening at http://localhost:8080`)
})