mirror of
https://gitee.com/eolink_admin/postcat.git
synced 2024-12-02 03:38:03 +08:00
feat: local start socket server automatical
This commit is contained in:
parent
0fac2313f9
commit
579b44d71d
@ -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 = {
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
88
src/workbench/node/server/socketio.js
Normal file
88
src/workbench/node/server/socketio.js
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user