diff --git a/config.json b/config.json index 864e149..f0b20ee 100644 --- a/config.json +++ b/config.json @@ -21,8 +21,16 @@ "cli": "python3" } }, - "tempPath": "./temp", - "clientPath": "./mixly", - "mode": "web-socket", - "port": 4000 + "client": { + "path": "./mixly", + "port": 4000 + }, + "server": { + "path": { + "temp": "./temp" + }, + "mode": "all", + "protocol": "wss", + "port": 4000 + } } \ No newline at end of file diff --git a/src/common/config.js b/src/common/config.js index 60de31c..8542424 100644 --- a/src/common/config.js +++ b/src/common/config.js @@ -5,11 +5,14 @@ import fsExtra from 'fs-extra'; function processConfig(data) { for (let i in data.path) { - if (data.path[i] instanceof String) { + if (!data.path[i]) { + continue; + } + if (typeof data.path[i] === 'string') { data.path[i] = path.resolve(process.cwd(), data.path[i]); - } else if (data.path[i] instanceof Array) { + } else if (typeof data.path[i] === 'object') { for (let j in data.path[i]) { - if (!(data.path[i][j] instanceof String)) { + if (typeof data.path[i][j] !== 'string') { continue; } data.path[i][j] = path.resolve(process.cwd(), data.path[i][j]); @@ -25,8 +28,10 @@ export const ARDUINO = processConfig(CONFIG.arduino); export const MICROPYTHON = processConfig(CONFIG.micropython); export const PYTHON = processConfig(CONFIG.python); export const CURRENT_PLANTFORM = os.platform(); -export const TEMP_PATH = path.resolve(process.cwd(), CONFIG.tempPath); -export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.clientPath); +export const TEMP_PATH = path.resolve(process.cwd(), CONFIG.server.path.temp); +export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.client.path); export const CERTS_PATH = path.resolve(process.cwd(), 'certs'); -export const PORT = CONFIG.port; -export const MODE = CONFIG.mode; \ No newline at end of file +export const CLIENT_PORT = CONFIG.client.port; +export const SERVER_PORT = CONFIG.server.protocol === 'wss' ? CLIENT_PORT : CONFIG.server.port; +export const SERVER_MODE = CONFIG.server.mode; +export const SERVER_PROTOCOL = CONFIG.server.protocol; \ No newline at end of file diff --git a/src/common/socket.js b/src/common/socket.js index 349de92..a66b073 100644 --- a/src/common/socket.js +++ b/src/common/socket.js @@ -23,8 +23,8 @@ export default class Socket { #shellArduino_ = new Registry(); #shellAmpy_ = new Registry(); - constructor(httpsServer, options) { - this.#io_ = new Server(httpsServer, options); + constructor(server, options) { + this.#io_ = new Server(server, options); this.#namespaceAll_ = this.#io_.of('/all'); this.#namespaceCompile_ = this.#io_.of('/compile'); this.#namespaceCompile_.on('connection', (socket) => { diff --git a/src/index.js b/src/index.js index d28dabf..22d79d0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,11 @@ -import { createServer } from 'node:https'; +import https from 'node:https'; +import http from 'node:http'; import path from 'node:path'; import fsExtra from 'fs-extra'; import express from 'express'; import Socket from './common/socket.js'; import { getCertificate } from './common/utils.js'; -import { CLIENT_PATH, PORT, MODE } from './common/config.js'; +import { CLIENT_PATH, CLIENT_PORT, SERVER_PORT, SERVER_MODE, SERVER_PROTOCOL } from './common/config.js'; process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; @@ -12,17 +13,20 @@ const app = express(); if (CLIENT_PATH) { app.use(express.static(CLIENT_PATH)); } -const httpsServer = createServer(getCertificate(), app); - -if (MODE !== 'static') { - const socket = new Socket(httpsServer, { +const httpsServer = https.createServer(getCertificate(), app); +let httpServer = null; +if (CLIENT_PORT !== SERVER_PORT) { + httpServer = http.createServer() +} +if (SERVER_MODE !== 'static') { + const socket = new Socket(httpServer ? httpServer : httpsServer, { path: '/mixly-socket/', maxHttpBufferSize: 1e8, cors: { origin: '*', methods: ['GET', 'POST'], transports: ['websocket', 'polling', 'flashsocket'], - credentials: true + credentials: false } }); @@ -46,13 +50,13 @@ if (MODE !== 'static') { const mixlyConfigPath = path.resolve(CLIENT_PATH, 'sw-config.json'); const mixlyConfig = fsExtra.readJSONSync(mixlyConfigPath); -if (MODE === 'web-compiler') { +if (SERVER_MODE === 'compiler') { mixlyConfig['webCompiler']['enabled'] = true; - mixlyConfig['webCompiler']['url'] = `wss://127.0.0.1:${PORT}`; + mixlyConfig['webCompiler']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`; mixlyConfig['webSocket']['enabled'] = false; -} else if (MODE === 'web-socket') { +} else if (SERVER_MODE === 'all') { mixlyConfig['webCompiler']['enabled'] = false; - mixlyConfig['webSocket']['url'] = `wss://127.0.0.1:${PORT}`; + mixlyConfig['webSocket']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`; mixlyConfig['webSocket']['enabled'] = true; } else { mixlyConfig['webCompiler']['enabled'] = false; @@ -63,10 +67,13 @@ fsExtra.writeJSONSync(mixlyConfigPath, mixlyConfig, { spaces: ' ' }); -httpsServer.listen(PORT); -if (CLIENT_PATH) { - console.log(`Static服务器正在运行: https://127.0.0.1:${PORT}`); +httpsServer.listen(CLIENT_PORT); +if (httpServer) { + httpServer.listen(SERVER_PORT); } -if (MODE !== 'static') { - console.log(`Socket.io服务器正在运行: wss://127.0.0.1:${PORT}/mixly-socket`); +if (CLIENT_PATH) { + console.log(`Static服务器正在运行: https://127.0.0.1:${CLIENT_PORT}`); +} +if (SERVER_MODE !== 'static') { + console.log(`Socket.io服务器正在运行: ${SERVER_PROTOCOL}://127.0.0.1:${SERVER_PORT}`); } \ No newline at end of file