// electron/electron.js const path = require('path'); const { app, BrowserWindow, ipcMain } = require('electron'); const isDev = process.env.IS_DEV == 'true' ? true : false; // 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', }; } }); /** * 跳转程序 url */ ipcMain.on('jumpTo', (event, url) => { console.log("jumpTo",event, url); // if (newWin) { // newWin.focus() // 存在 则聚焦 // return // } const newWin = new BrowserWindow({ width: 900, height: 620, minWidth: 900, minHeight: 620, frame: true, //是否显示边缘框 fullscreen: false, //是否全屏显示 title: "项目名", autoHideMenuBar: false, webPreferences: { javascript: true, plugins: true, allowRunningInsecureContent: true, // 允许web端使用node nodeIntegration: true, contextIsolation: false, // 同源策略关闭 webSecurity: false, } }) // newWin.loadURL(winURL + '#/路由地址') // 此处写 你要打开的路由地址 newWin.loadURL(url) // 此处写 你要打开的路由地址 // newWin.on('close', () => { // newWin = null // }) }); /////////////////////////////////////////// function createWindow() { // Create the browser window. const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { // preload: path.join(__dirname, 'preload.js'), javascript: true, plugins: true, allowRunningInsecureContent: true, // 允许web端使用node nodeIntegration: true, contextIsolation: false, // 同源策略关闭 webSecurity: false, }, }); // 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')}`); mainWindow.loadURL('http://localhost:3200'); // Open the DevTools. // if (isDev) { mainWindow.webContents.openDevTools(); // } } // 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(); 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(); }); }); // 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) // ` // ); // }); // }); // });