乐趣区

electron 网页和主进程通讯

前端网页
import {
RxIpc
} from ‘rx-ipc-electron/lib/rx-ipc’;

export const IsElectron = window.navigator.userAgent.toLowerCase().indexOf(‘electron’) !== -1;

export function ajax(config) {
if (!IsElectron) return Promise.reject(‘only for electron’);

const {
ipcRenderer
} = eval(`require(‘electron’)`);
const rxIpc = new RxIpc(ipcRenderer);

return new Promise((resolve, reject) => {
rxIpc.runCommand(‘ajax’, null, config).subscribe(resolve, reject);
});
}

window[‘test1’] = async function test1() {
const url = ‘https://www.baidu.com/s’;

const params = {
wd: 1
};

const rsp = await ajax({
url,
method: ‘get’,
params,
});

console.log(rsp);
}

electron 进程
import rxIpc from ‘rx-ipc-electron/lib/main’;
import {Observable} from ‘rxjs’;
import axios from ‘axios’;

rxIpc.registerListener(‘ajax’, config => {
return Observable.from(axios(config));
});

退出移动版