Skip to content

Commit

Permalink
feat: local start socket server automatical
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfuboy committed Sep 9, 2022
1 parent 0fac231 commit 579b44d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 88 deletions.
3 changes: 3 additions & 0 deletions src/app/electron-main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
1 change: 0 additions & 1 deletion src/workbench/browser/src/environments/environment.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
86 changes: 0 additions & 86 deletions src/workbench/node/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
});
});
3 changes: 2 additions & 1 deletion src/workbench/node/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -17,4 +18,4 @@
"koa-body": "5.0.0",
"pm2": "5.2.0"
}
}
}
88 changes: 88 additions & 0 deletions src/workbench/node/server/socketio.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 579b44d

Please sign in to comment.