const {app, BrowserWindow} = require('electron')
const ipcMain = require('electron').ipcMain
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
const Host = 'http://192.168.2.159';
//进程窗口
//主窗口
let win;
//三维进程
let unity3d;
//在三维中弹出的新窗口
let win3d;
//创建主窗口
function createMainWindow () {
// 创建浏览器窗口。
win = new BrowserWindow({frame: false,kiosk : true,backgroundColor:'#2e2c29'})
//win = new BrowserWindow({width:800,height:600})
// 然后加载应用的 index.html。
//win.loadFile('index.html')
win.loadURL(Host);
//win.loadURL(Host + '/htm/battery/battery.html');
//win.loadURL('http://47.93.197.116/');
// Open the DevTools.
//win.webContents.openDevTools()
// 当 window 被关闭,这个事件会被触发。
win.on('closed', () => {
// 取消引用 window 对象,如果你的应用支持多窗口的话,
// 通常会把多个 window 对象存放在一个数组里面,
// 与此同时,你应该删除相应的元素。
win = null
})
}
// Electron 会在初始化后并准备
// 创建浏览器窗口时,调用这个函数。
// 部分 API 在 ready 事件触发后才能使用。
app.on('ready', createMainWindow)
// 当全部窗口关闭时退出。
app.on('window-all-closed', () => {
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
// 否则绝大部分应用及其菜单栏会保持激活。
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// 在macOS上,当单击dock图标并且没有其他窗口打开时,
// 通常在应用程序中重新创建一个窗口。
if (win === null) {
createMainWindow()
}
})
//请求启动三维进程
ipcMain.on('require-3d',function(event, arg0, arg1, arg2){
// console.log(arg0);
// console.log(arg1);
// console.log(arg2);
//开启新的子进程
const spawn = require('child_process').spawn;
//http服务,与unity通讯
const http = require('http');
let server = null;
//解析unity发起的http请求地址
var url = require('url');
var postCommand = [arg0,arg1];
if(undefined != arg2){
postCommand.push(arg2);
}
unity3d = spawn('exe/StartSubject.exe',postCommand);//使用shell方法指定一个shell选项
unity3d.stdout.on('data', function(data){
console.log(data);
console.log("三维准备启动");
});
unity3d.stderr.on('data', function(data){
console.log(data);
});
unity3d.on('exit', function(code){
win.show();
console.log(`Child exited with code ${code}`);
//通知主进程,unity已被关闭
//ipc.send('i-am-close', 'ping');
//关闭掉通讯服务器
server.close();
//通知渲染进程三维关闭
event.sender.send('3disClose');
win.setAlwaysOnTop(true);
unity3d = null;
});
//开启http服务
server = http.createServer(function(request, response){
//解析请求地址,获取参数
var urlObj = url.parse(request.url,true,false);
console.log(urlObj.pathname);
switch(urlObj.pathname){
case "/ok":
//发送响应
response.writeHead(200, { 'Content-Type': 'text-plain' });
response.end('好的\n');
event.sender.send('3disOk');
console.log("三维启动成功");
//unity请求nodejs的http,说明unity已正常启动,此时通知主进程
//ipc.send('i-am-ready', 'ping');
unity3d.setAlwaysOnTop(true);
setTimeout(function(){
win.hide();
},1000);
break;
case "/open2d":
// 创建浏览器窗口
win3d = new BrowserWindow({frame: false,kiosk : true,backgroundColor:'#2e2c29'})
//win = new BrowserWindow({width:800,height:600})
// 然后加载应用的 index.html。
win3d.loadURL(Host + '/htm/index.html');
//win.loadURL('http://192.168.1.199');
//win.loadURL('http://47.93.197.116/');
// Open the DevTools.
//win.webContents.openDevTools()
// 当 window 被关闭,这个事件会被触发。
win3d.on('closed', () => {
// 取消引用 window 对象,如果你的应用支持多窗口的话,
// 通常会把多个 window 对象存放在一个数组里面,
// 与此同时,你应该删除相应的元素。
win3d = null
});
win3d.setAlwaysOnTop(true);
break;
}
}).listen(9527);
})
// //三维进程初始化完成
// ipc.on('i-am-ready', function (event, arg) {
// console.log("i-am-ready");
// //隐藏掉主窗口
// win.hide();
// })
// //三维进程结束
// ipc.on('i-am-close', function (event, arg) {
// console.log("i-am-close");
// //恢复主窗口
// win.show();
// win.restore();
// })
// 在这个文件中,你可以续写应用剩下主进程代码。
// 也可以拆分成几个文件,然后用 require 导入。
//npm run-script packageWin
原文地址:https://blog.csdn.net/niujing1987/article/details/100738843
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。