数字孪生Web 后台dt( digital twin)2.0版本 统一命名格式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.3 KiB

10 months ago
// electron/electron.js
const path = require('path');
const { app, BrowserWindow, ipcMain } = require('electron');
10 months ago
const isDev = process.env.IS_DEV == 'true' ? true : false;
10 months ago
// rtsp相关,ffmpeg-static会自动下载对应平台windos,linux,mac(intle),mac(M1版本)的ffmpeg二进制文件
const ffmpegPath = require('ffmpeg-static');
/**
stream相关配置及方法
name = options.name
streamUrl = options.streamUrl
width = options.width
height = options.height
wsPort = options.wsPort
nputStreamStarted = false
stream = undefined
ffmpegPath = options?.ffmpegPath ?? ffmpeg
stop()
*/
const Stream = require('node-rtsp-stream');
/**
* rtsp列表
* interface {
* rtspUrl: {
* ws: websocket地址
* stream: stream实例
* }
* }
*/
const rtspOpenders = {};
let addPort = 9000;
/**
* 开启rtsp
* @param rtsp {String} rtsp流地址
*/
ipcMain.on('openRtsp', (event, rtsp) => {
/** 判断是否已开启,若已开启,直接返回ws地址, 未开启则先开启再返回 */
if (rtspOpenders[rtsp]) {
event.returnValue = {
code: 200,
msg: '开启成功',
ws: rtspOpenders[rtsp].ws,
};
} else {
addPort++;
const stream = new Stream({
name: `socket-${addPort}`,
streamUrl: rtsp,
wsPort: addPort,
ffmpegPath: app.isPackaged ? ffmpegPath.replace('app.asar', 'app.asar.unpacked') : ffmpegPath,
ffmpegOptions: {
'-stats': '',
'-r': 30,
},
}).on('exitWithError', () => {
stream.stop();
delete rtspOpenders[rtsp];
event.returnValue = {
code: 400,
msg: '开启失败',
};
});
rtspOpenders[rtsp] = {
ws: `ws://localhost:${addPort}`,
stream: stream,
};
event.returnValue = {
code: 200,
msg: '开启成功',
ws: rtspOpenders[rtsp].ws,
};
}
});
/**
* 关闭rtsp
*/
ipcMain.on('closeRtsp', (event, rtsp) => {
if (rtspOpenders[rtsp]) {
// 停止解析
rtspOpenders[rtsp].stream.stop();
// 删除该项
delete rtspOpenders[rtsp];
// 返回结果
event.returnValue = {
code: 200,
msg: '关闭成功',
};
} else {
event.returnValue = {
code: 400,
msg: '未找到该rtsp',
};
}
});
///////////////////////////////////////////
10 months ago
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// preload: path.join(__dirname, 'preload.js'),
10 months ago
javascript: true,
plugins: true,
allowRunningInsecureContent: true,
// 允许web端使用node
10 months ago
nodeIntegration: true,
contextIsolation: false,
// 同源策略关闭
webSecurity: false,
10 months ago
},
});
// and load the index.html of the app.
// win.loadFile("index.html");
mainWindow.loadURL(isDev ? 'http://localhost:3200' : `file://${path.join(__dirname, '../dist/index.html')}`);
10 months ago
// mainWindow.loadURL('http://127.0.0.1:5500');
// Open the DevTools.
// if (isDev) {
mainWindow.webContents.openDevTools();
10 months ago
// }
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
10 months ago
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
10 months ago
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// // 在主进程中监听 webContents 的 did-create-window 事件
// app.on('web-contents-created', (event, webContents) => {
// // 监听新窗口创建事件
// webContents.on('did-create-window', (Window, details) => {
// console.log('Window---', Window);
// // 在新窗口创建完成后重新加载预加载脚本
// Window.webContents.on('dom-ready', () => {
// Window.webContents.executeJavaScript(
// `
// console.log('window----',window)
// `
// );
// });
// });
// });