Commit dc682150 authored by 王曜嵚 Wang Yaoqin's avatar 王曜嵚 Wang Yaoqin

dev: websocket

parent 5b640397
...@@ -2,5 +2,10 @@ ...@@ -2,5 +2,10 @@
export default defineNuxtConfig({ export default defineNuxtConfig({
devtools: { enabled: true }, devtools: { enabled: true },
modules: ['@pinia/nuxt'], modules: ['@pinia/nuxt'],
css: ['~/assets/css/main.css', '~/assets/css/color.css'] css: ['~/assets/css/main.css', '~/assets/css/color.css'],
nitro: {
experimental: {
websocket: true
}
}
}) })
This diff is collapsed.
...@@ -12,10 +12,13 @@ ...@@ -12,10 +12,13 @@
"dependencies": { "dependencies": {
"@pinia/nuxt": "^0.5.1", "@pinia/nuxt": "^0.5.1",
"dockerode": "^3.3.5", "dockerode": "^3.3.5",
"engine.io": "^6.5.4",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"nuxt": "^3.11.1", "nuxt": "^3.11.1",
"pg": "^8.11.5", "pg": "^8.11.5",
"pinia": "^2.1.7", "pinia": "^2.1.7",
"socket.io": "^4.7.5",
"socket.io-client": "^4.7.5",
"tar-fs": "^3.0.5", "tar-fs": "^3.0.5",
"telnet-client": "^2.2.0", "telnet-client": "^2.2.0",
"vue": "^3.4.21", "vue": "^3.4.21",
......
...@@ -98,6 +98,7 @@ ...@@ -98,6 +98,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useUserStore } from '~/store/user'; import { useUserStore } from '~/store/user';
import { io } from 'socket.io-client';
const username = ref(useUserStore().username) const username = ref(useUserStore().username)
...@@ -105,7 +106,7 @@ const status = ref('null') // null | created | stopped | running | creating | co ...@@ -105,7 +106,7 @@ const status = ref('null') // null | created | stopped | running | creating | co
const statusV1 = ref('null') const statusV1 = ref('null')
const loading = ref(false) const loading = ref(false)
const logLevel = ref('progress') // info | progress const logLevel = ref('progress') // info | progress
const logs = ref([]) const logs = ref([] as string[])
const port = ref(0) const port = ref(0)
const renderLogs = computed(() => { const renderLogs = computed(() => {
...@@ -114,29 +115,25 @@ const renderLogs = computed(() => { ...@@ -114,29 +115,25 @@ const renderLogs = computed(() => {
}) })
onMounted(() => { onMounted(() => {
// socket = io({ const socket = io({
// path: '/api/socket', path: '/socket.io',
// auth: { auth: {
// username: username username: username.value
// } }
// }); });
// // eslint-disable-next-line no-control-regex // eslint-disable-next-line no-control-regex
// const removeLogPrefix = (log) => log.replace(/^\[.*?\]\s/, '').replace(/[\u0000-\u0009]/g, '').replace(/[\u000B-\u001F]/g, '') const removeLogPrefix = (log: string) => log.replace(/^\[.*?\]\s/, '').replace(/[\u0000-\u0009]/g, '').replace(/[\u000B-\u001F]/g, '')
// const replaceLineFlag = (log) => log.replaceAll('\n', '<br />') const replaceLineFlag = (log: string) => log.replaceAll('\n', '<br />')
// socket.on('Log', (log) => { socket.on('Log', (log) => {
// if (logLevel === 'info') { if (logLevel.value === 'info') {
// logs.push(replaceLineFlag(removeLogPrefix(log))) logs.value.push(replaceLineFlag(removeLogPrefix(log)))
// } else { } else {
// if (!log.startsWith('[info]')) { if (!log.startsWith('[info]')) {
// logs.push(replaceLineFlag(removeLogPrefix(log))) logs.value.push(replaceLineFlag(removeLogPrefix(log)))
// } }
// } }
// if (log === '[progress] [[1;31mSuccess[m]] installed') { })
// loading = false
// handleGetStatus()
// }
// })
handleGetStatus() handleGetStatus()
checkAndRestartDocker() checkAndRestartDocker()
...@@ -284,7 +281,7 @@ function handleDebug() { ...@@ -284,7 +281,7 @@ function handleDebug() {
}) })
} }
function test() { function test() {
$fetch('/api/test/putArchive', { method: 'post' }).then(() => { $fetch('/api/devops/v2/test').then(() => {
alert('测试成功') alert('测试成功')
}).catch(() => { }).catch(() => {
alert('测试失败') alert('测试失败')
......
...@@ -3,8 +3,6 @@ import LogUtil from "~/server/utils/log" ...@@ -3,8 +3,6 @@ import LogUtil from "~/server/utils/log"
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
try { try {
let username = event.context.username let username = event.context.username
let docker = createDockerFactory(username)
let container = await docker.checkContainer('logwire_backend_helper.' + username + '.node')
LogUtil.printInfo(username, '测试消息') LogUtil.printInfo(username, '测试消息')
} catch (err) { } catch (err) {
setResponseStatus(event, 500) setResponseStatus(event, 500)
......
import type { NitroApp } from "nitropack";
import { Server as Engine } from "engine.io";
import { Server } from "socket.io";
import { defineEventHandler } from "h3";
import { setWebsocketIo } from "../utils/log";
export default defineNitroPlugin((nitroApp: NitroApp) => {
const engine = new Engine();
const io = new Server();
io.bind(engine);
io.on("connection", (socket: any) => {
let username = socket.handshake.auth['username']
setWebsocketIo(username, socket)
});
nitroApp.router.use("/socket.io/", defineEventHandler({
handler(event) {
engine.handleRequest(event.node.req, event.node.res);
event._handled = true;
},
websocket: {
open(peer) {
const nodeContext = peer.ctx.node;
const req = nodeContext.req;
// @ts-expect-error private method
engine.prepare(req);
const rawSocket = nodeContext.req.socket;
const websocket = nodeContext.ws;
// @ts-expect-error private method
engine.onWebSocket(req, rawSocket, websocket);
}
}
}));
});
\ No newline at end of file
...@@ -5,43 +5,43 @@ ...@@ -5,43 +5,43 @@
import { setUserConfig, getUserConfig } from "./server" import { setUserConfig, getUserConfig } from "./server"
// import { Socket } from "socket.io/dist/socket"; import { Socket } from "socket.io";
// let websocket: Map<string, Socket> = new Map() let websocket: Map<string, Socket> = new Map()
// export function setWebsocketIo (username: string, io: Socket) { export function setWebsocketIo (username: string, io: Socket) {
// websocket.set(username, io) websocket.set(username, io)
// } }
// export function getWebsocketIo (username: string) { export function getWebsocketIo (username: string) {
// return websocket.get(username) as Socket return websocket.get(username) as Socket
// } }
export default class LogUtil { export default class LogUtil {
// 根据 log 判断用户是否已经执行过 // 根据 log 判断用户是否已经执行过
static async run (username: string, log: string, cb: () => Promise<void>) { static async run (username: string, log: string, cb: () => Promise<void>) {
let key = 'InstallSteps' let key = 'InstallSteps'
let steps: string[] = getUserConfig(username, key) || [] let steps: string[] = getUserConfig(username, key) || []
// let socket = getWebsocketIo(username) let socket = getWebsocketIo(username)
// socket.emit('Log', '[progress] [1m[Loading][m ' + log + '中...\n') socket.emit('Log', '[progress] [1m[Loading][m ' + log + '中...\n')
await cb() await cb()
steps.push(log) steps.push(log)
setUserConfig(username, key, steps) setUserConfig(username, key, steps)
// socket.emit('Log', '[progress] [1m[Info][m ' + log + '完成\n') socket.emit('Log', '[progress] [1m[Info][m ' + log + '完成\n')
} }
static async print(username: string, log: string) { static async print(username: string, log: string) {
// let socket = getWebsocketIo(username) let socket = getWebsocketIo(username)
// socket.emit('Log', removeUnreadCharacter(log)) socket.emit('Log', removeUnreadCharacter(log))
} }
static async printInfo(username: string, log: string) { static async printInfo(username: string, log: string) {
// let socket = getWebsocketIo(username) let socket = getWebsocketIo(username)
// socket.emit('Log', removeUnreadCharacter(`[progress] [[1;34mInfo[m] ${log} \n`)) socket.emit('Log', removeUnreadCharacter(`[progress] [[1;34mInfo[m] ${log} \n`))
} }
static async printError(username: string, log: string) { static async printError(username: string, log: string) {
// let socket = getWebsocketIo(username) let socket = getWebsocketIo(username)
// socket.emit('Log', removeUnreadCharacter(`[error] [[1;31mError[m] ${log} \n`)) socket.emit('Log', removeUnreadCharacter(`[error] [[1;31mError[m] ${log} \n`))
} }
static async printSuccess(username: string, log: string) { static async printSuccess(username: string, log: string) {
// let socket = getWebsocketIo(username) let socket = getWebsocketIo(username)
// socket.emit('Log', removeUnreadCharacter(`[progress] [[1;31mSuccess[m] ${log} \n`)) socket.emit('Log', removeUnreadCharacter(`[progress] [[1;31mSuccess[m] ${log} \n`))
} }
static async printWarning (username: string, log: string) { static async printWarning (username: string, log: string) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment