electronJS 开发linux App

 

      electronJS 是一个跨平台软件开发工具,使用 JavaScript, HTML 和 CSS 构建跨平台的桌面应用。由于使用了HTML 所以开发的界面更加丰富,美观和快捷。缺点是哪怕是一个Hello the world,也有几百M,程序很庞大。个人觉得electronJS 适合界面要求比较高的App开发。

使用electron开发的App可以在windows,iOS,linux,android 上运行。

安装 electron

  electron 基于nodeJS 的引擎。所以在安装electron之前要安装nodeJS

安装electron 最简单的方式是安装一个electron-quick-start

# 克隆这仓库
$ git clone https://github.com/electron/electron-quick-start
# 进入仓库
$ cd electron-quick-start
# 安装依赖库
$ npm install
# 运行应用
$ npm start

主进程和渲染进程

运行mainJS 进程的是主进程,显示网页进程为渲染进程。可以想象为主进程是nodeJS,渲染进程是浏览器。

主进程和渲染进程之间的消息通信机制

main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const {ipcMain}=require('electron')

// 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.
let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js')
    }
  })
  mainWindow.removeMenu();
  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  mainWindow.webContents.openDevTools()
// Event handler for asynchronous incoming messages
ipcMain.on('asynchronous-message', (event, arg) => {
  console.log(arg)

  // Event emitter for sending asynchronous messages
  event.sender.send('asynchronous-reply', 'async pong')
})

// Event handler for synchronous incoming messages
ipcMain.on('synchronous-message', (event, arg) => {
  console.log(arg) 

  // Synchronous event emmision
  event.returnValue = 'sync pong'
})
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// 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.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})

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 (mainWindow === null) createWindow()
})

render.js 

const {ipcRenderer} = require('electron')
         // Synchronous message emmiter and handler
         console.log(ipcRenderer.sendSync('synchronous-message', 'sync ping')) 

         // Async message handler
         ipcRenderer.on('asynchronous-reply', (event, arg) => {
            console.log(arg)
         })

         // Async message sender
ipcRenderer.send('asynchronous-message', 'async ping')

主线程向渲染进程主动发送消息

 

1 只有在窗口建立以后,才能向渲染进程发送消息

mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('asynchronous-message','Hello');
})

2 加个延时就可以了

setInterval(function(){
  mainWindow.webContents.send('asynchronous-message','Hello');
},1000);

调用websocket

你可能感兴趣的:(electronJS 开发linux App)