diff --git a/src/app/electron-main/main.ts b/src/app/electron-main/main.ts index c3c066f4e..51c13a723 100644 --- a/src/app/electron-main/main.ts +++ b/src/app/electron-main/main.ts @@ -13,6 +13,7 @@ import { UnitWorkerModule } from '../../workbench/node/electron/main'; import Configuration from '../../platform/node/configuration/lib'; import { ConfigurationInterface } from 'src/platform/node/configuration'; import { MockServer } from 'eo/platform/node/mock-server'; +import socket from '../../workbench/node/server/socketio'; import { LanguageService } from 'eo/app/electron-main/language.service'; export const subView = { @@ -36,6 +37,8 @@ if (app.isPackaged) { const eoUpdater = new EoUpdater(); const mockServer = new MockServer(); +// * start SocketIO +socket(); const moduleManager: ModuleManagerInterface = new ModuleManager(); const configuration: ConfigurationInterface = Configuration(); global.shareObject = { diff --git a/src/workbench/browser/src/environments/environment.dev.ts b/src/workbench/browser/src/environments/environment.dev.ts index 428a5fd54..0914db057 100644 --- a/src/workbench/browser/src/environments/environment.dev.ts +++ b/src/workbench/browser/src/environments/environment.dev.ts @@ -4,7 +4,6 @@ export const APP_CONFIG = Object.assign( production: false, environment: 'DEV', SOCKETIO_URL: 'ws://localhost:4301', - // SOCKETIO_URL: 'ws://106.12.149.147:4301', }, COMMON_CONFIG ); diff --git a/src/workbench/node/server/main.js b/src/workbench/node/server/main.js index d616e57f8..456e12956 100644 --- a/src/workbench/node/server/main.js +++ b/src/workbench/node/server/main.js @@ -9,11 +9,6 @@ const koaBody = require('koa-body'); const Koa = require('koa'); const cors = require('@koa/cors'); -const IO = require('socket.io'); -const WebSocket = require('ws'); - -const io = new IO.Server(4301); - const app = new Koa(); const port = 4201; @@ -50,84 +45,3 @@ app.use(async (ctx, next) => { }); app.listen(port); - -io.on('connection', (socket) => { - // send a message to the client - socket.emit('ws-client', 'link success'); - let ws = null; - - // receive a message from the client - socket.on('ws-server', ({ type, content }) => { - if (type === 'connect') { - return; - } - if (type === 'ws-disconnect') { - ws = null; - return; - } - if (type === 'ws-connect') { - const { request } = content; - // console.log(request?.requestHeaders); - // try { - // new WebSocket(request.uri); - // } catch (error) { - // console.log('try to get the error', error); - // } - try { - ws = new WebSocket(request.protocol + request.uri, { - headers: request?.requestHeaders - ?.filter((it) => it.name && it.value) - .reduce( - (total, { name, value }) => ({ - ...total, - [name]: value, - }), - {} - ), - }); - ws.on('error', (err) => { - socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err }); - }); - } catch (error) { - socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error }); - ws = null; - return; - } - - const reqHeader = ws._req.getHeaders(); - // console.log(ws); - - // 打开WebSocket连接后立刻发送一条消息: - ws.on('open', () => { - // console.log(`[CLIENT] open()`); - }); - ws.on('upgrade', (res) => { - const { headers: resHeader } = res; - socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } }); - }); - - ws.on('message', (message) => { - socket.emit('ws-client', { - type: 'ws-message-back', - status: 0, - content: message, - }); - }); - - ws.on('close', () => { - socket.emit('ws-client', { - type: 'ws-message-back', - status: 1, - content: 'Server disconnected', - }); - }); - } - if (type === 'ws-message') { - const { message } = content; - if (!message) { - console.log('发送内容为空'); - } - ws.send(message); - } - }); -}); diff --git a/src/workbench/node/server/package.json b/src/workbench/node/server/package.json index d52ae6d3b..bcb25870e 100644 --- a/src/workbench/node/server/package.json +++ b/src/workbench/node/server/package.json @@ -6,6 +6,7 @@ "scripts": { "dev": "node main.js", "start": "pm2 start main.js", + "start:io": "pm2 start socketio.js", "stop": "pm2 stop all", "test": "echo \"Error: no test specified\" && exit 1" }, @@ -17,4 +18,4 @@ "koa-body": "5.0.0", "pm2": "5.2.0" } -} +} \ No newline at end of file diff --git a/src/workbench/node/server/socketio.js b/src/workbench/node/server/socketio.js new file mode 100644 index 000000000..fd33d2ef7 --- /dev/null +++ b/src/workbench/node/server/socketio.js @@ -0,0 +1,88 @@ +const IO = require('socket.io'); +const WebSocket = require('ws'); + +const socket = (port = 4301) => { + const io = new IO.Server(port); + io.on('connection', (socket) => { + // send a message to the client + socket.emit('ws-client', 'link success'); + let ws = null; + + // receive a message from the client + socket.on('ws-server', ({ type, content }) => { + if (type === 'connect') { + return; + } + if (type === 'ws-disconnect') { + ws = null; + return; + } + if (type === 'ws-connect') { + const { request } = content; + // console.log(request?.requestHeaders); + // try { + // new WebSocket(request.uri); + // } catch (error) { + // console.log('try to get the error', error); + // } + try { + ws = new WebSocket(request.protocol + request.uri, { + headers: request?.requestHeaders + ?.filter((it) => it.name && it.value) + .reduce( + (total, { name, value }) => ({ + ...total, + [name]: value, + }), + {} + ), + }); + ws.on('error', (err) => { + socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: err }); + }); + } catch (error) { + socket.emit('ws-client', { type: 'ws-connect-back', status: -1, content: error }); + ws = null; + return; + } + + const reqHeader = ws._req.getHeaders(); + // console.log(ws); + + // 打开WebSocket连接后立刻发送一条消息: + ws.on('open', () => { + // console.log(`[CLIENT] open()`); + }); + ws.on('upgrade', (res) => { + const { headers: resHeader } = res; + socket.emit('ws-client', { type: 'ws-connect-back', status: 0, content: { reqHeader, resHeader } }); + }); + + ws.on('message', (message) => { + socket.emit('ws-client', { + type: 'ws-message-back', + status: 0, + content: message, + }); + }); + + ws.on('close', () => { + socket.emit('ws-client', { + type: 'ws-message-back', + status: 1, + content: 'Server disconnected', + }); + }); + } + if (type === 'ws-message') { + const { message } = content; + if (!message) { + console.log('发送内容为空'); + } + ws.send(message); + } + }); + }); +}; + +module.exports = socket;