diff --git a/packages/core/cli/src/commands/dev.js b/packages/core/cli/src/commands/dev.js index 4a0d1aa6bc..d486f9eefe 100644 --- a/packages/core/cli/src/commands/dev.js +++ b/packages/core/cli/src/commands/dev.js @@ -106,7 +106,9 @@ module.exports = (cli) => { env: { PORT: clientPort, APP_ROOT: `${APP_PACKAGE_ROOT}/client`, - WEBSOCKET_URL: process.env.WEBSOCKET_URL || (serverPort ? `ws://localhost:${serverPort}/ws` : undefined), + WEBSOCKET_URL: + process.env.WEBSOCKET_URL || + (serverPort ? `ws://localhost:${serverPort}${process.env.WS_PATH}` : undefined), PROXY_TARGET_URL: process.env.PROXY_TARGET_URL || (serverPort ? `http://127.0.0.1:${serverPort}` : undefined), }, diff --git a/packages/core/cli/src/util.js b/packages/core/cli/src/util.js index 23a84f2d2f..04cb6aa186 100644 --- a/packages/core/cli/src/util.js +++ b/packages/core/cli/src/util.js @@ -271,6 +271,7 @@ exports.initEnv = function initEnv() { LOCAL_STORAGE_DEST: 'storage/uploads', PLUGIN_STORAGE_PATH: resolve(process.cwd(), 'storage/plugins'), MFSU_AD: 'none', + WS_PATH: '/ws', NODE_MODULES_PATH: resolve(process.cwd(), 'node_modules'), PM2_HOME: resolve(process.cwd(), './storage/.pm2'), PLUGIN_PACKAGE_PREFIX: '@nocobase/plugin-,@nocobase/plugin-sample-,@nocobase/preset-', diff --git a/packages/core/client/src/application/WebSocketClient.ts b/packages/core/client/src/application/WebSocketClient.ts index 3ce776f6e3..af662402aa 100644 --- a/packages/core/client/src/application/WebSocketClient.ts +++ b/packages/core/client/src/application/WebSocketClient.ts @@ -7,18 +7,20 @@ export const getWebSocketURL = () => { } const subApp = getSubAppName(); const queryString = subApp ? `?__appName=${subApp}` : ''; + const wsPath = process.env.WS_PATH || '/ws'; if (process.env.WEBSOCKET_URL) { const url = new URL(process.env.WEBSOCKET_URL); if (url.hostname === 'localhost') { - return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${url.port}/ws${queryString}`; + const protocol = location.protocol === 'https:' ? 'wss' : 'ws'; + return `${protocol}://${location.hostname}:${url.port}${wsPath}${queryString}`; } return `${process.env.WEBSOCKET_URL}${queryString}`; } try { const url = new URL(process.env.API_BASE_URL); - return `${url.protocol === 'https:' ? 'wss' : 'ws'}://${url.host}/ws${queryString}`; + return `${url.protocol === 'https:' ? 'wss' : 'ws'}://${url.host}${wsPath}${queryString}`; } catch (error) { - return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/ws${queryString}`; + return `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}${wsPath}${queryString}`; } }; diff --git a/packages/core/devtools/umiConfig.js b/packages/core/devtools/umiConfig.js index 1605314437..0f08b7f03a 100644 --- a/packages/core/devtools/umiConfig.js +++ b/packages/core/devtools/umiConfig.js @@ -37,6 +37,7 @@ function getUmiConfig() { return memo; }, {}), define: { + 'process.env.WS_PATH': process.env.WS_PATH, 'process.env.API_BASE_URL': API_BASE_URL || API_BASE_PATH, 'process.env.APP_ENV': process.env.APP_ENV, 'process.env.VERSION': packageJson.version, diff --git a/packages/core/server/src/__tests__/gateway.test.ts b/packages/core/server/src/__tests__/gateway.test.ts index 7b894ecc9b..5aabc0288a 100644 --- a/packages/core/server/src/__tests__/gateway.test.ts +++ b/packages/core/server/src/__tests__/gateway.test.ts @@ -154,7 +154,7 @@ describe('gateway', () => { let port; let messages: Array; const connectClient = (port) => { - wsClient = new ws(`ws://localhost:${port}/ws`); + wsClient = new ws(`ws://localhost:${port}${process.env.WS_PATH}`); wsClient.on('message', (data) => { const message = data.toString(); messages.push(message); diff --git a/packages/core/server/src/gateway/index.ts b/packages/core/server/src/gateway/index.ts index 6cd1b5540a..6e76d21890 100644 --- a/packages/core/server/src/gateway/index.ts +++ b/packages/core/server/src/gateway/index.ts @@ -15,7 +15,7 @@ import handler from 'serve-handler'; import { parse } from 'url'; import { AppSupervisor } from '../app-supervisor'; import { ApplicationOptions } from '../application'; -import { getPackageDirByExposeUrl, getPackageNameByExposeUrl, PLUGIN_STATICS_PATH } from '../plugin-manager'; +import { PLUGIN_STATICS_PATH, getPackageDirByExposeUrl, getPackageNameByExposeUrl } from '../plugin-manager'; import { applyErrorWithArgs, getErrorWithCode } from './errors'; import { IPCSocketClient } from './ipc-socket-client'; import { IPCSocketServer } from './ipc-socket-server'; @@ -331,7 +331,7 @@ export class Gateway extends EventEmitter { from: 'node', }) .then(async () => { - if (!await mainApp.isStarted()) { + if (!(await mainApp.isStarted())) { await mainApp.stop(); } }) @@ -396,7 +396,7 @@ export class Gateway extends EventEmitter { this.server.on('upgrade', (request, socket, head) => { const { pathname } = parse(request.url); - if (pathname === '/ws') { + if (pathname === process.env.WS_PATH) { this.wsServer.wss.handleUpgrade(request, socket, head, (ws) => { this.wsServer.wss.emit('connection', ws, request); }); diff --git a/packages/core/test/src/server/index.ts b/packages/core/test/src/server/index.ts index 43453084dc..e0caf88225 100644 --- a/packages/core/test/src/server/index.ts +++ b/packages/core/test/src/server/index.ts @@ -32,9 +32,9 @@ export const startServerWithRandomPort = async (startServer) => { }; export const createWsClient = async ({ serverPort, options = {} }) => { - console.log(`connect to ws://localhost:${serverPort}/ws`, options); + console.log(`connect to ws://localhost:${serverPort}${process.env.WS_PATH}`, options); - const wsc = new ws(`ws://localhost:${serverPort}/ws`, options); + const wsc = new ws(`ws://localhost:${serverPort}${process.env.WS_PATH}`, options); const messages = []; wsc.on('message', (data) => {