Commit e4815de8 authored by rocosen's avatar rocosen

feat:ipc 通信

parent ec9da2bf
This diff was suppressed by a .gitattributes entry.
var net = require('net');
var os = require('os');
var events = require('events');
var ipc = require('./ipc');
var L = console.log;
ipc_one = ipc.getEvents()
ipc_one.on('connected', function (m) {
L("Client has connected")
});
ipc_one.on('data', function (m) {
L(m.type)
L(m.data.toString())
});
ipc_one.on('error', function (data) {
L(data.toString());
});
ipc_one.on('close', function (data) {
L(data.toString());
});
ipc.connect("testtest", true)
var a = setInterval(myTimer, 50);
function myTimer() {
if (ipc.status() == "Closed") {
clearInterval(a)
clearInterval(b)
}
L('Connecton status: ' + ipc.status())
ipc.write(4, "Hello server")
ipc.write(212, "Hello server")
ipc.write(663, "Hello server")
}
var b = setInterval(myTimerw, 8000);
function myTimerw() {
// ipc.close()
// clearInterval(myTimerw)
}
\ No newline at end of file
This diff is collapsed.
const child_process = require('child_process');
let child = child_process.spawn('go', ['run', 'gochild.go'], {
stdio: [0, 1, 2, 'ipc'],
});
child.on('close', (code) => {
process.exit(code);
});
child.send({ hello: 'child' });
child.on('message', function (msg, handle) {
if (msg.hello === 'parent') {
console.log(msg);
process.exit(0);
}
process.exit(1);
});
...@@ -19,6 +19,7 @@ const positioner = require('electron-traywindow-positioner'); ...@@ -19,6 +19,7 @@ const positioner = require('electron-traywindow-positioner');
const storage = require('electron-localstorage'); const storage = require('electron-localstorage');
const fs = require('fs'); const fs = require('fs');
const fse = require('fs-extra'); const fse = require('fs-extra');
const ipc = require('../commons/utils/ipc');
class AppUpdater { class AppUpdater {
constructor() { constructor() {
log.transports.file.level = 'info'; log.transports.file.level = 'info';
...@@ -126,7 +127,7 @@ ipcMain.on('resize-home', (event) => { ...@@ -126,7 +127,7 @@ ipcMain.on('resize-home', (event) => {
homeWindown.loadURL(resolveHtmlPath(`index.html#/home`)); homeWindown.loadURL(resolveHtmlPath(`index.html#/home`));
positioner.position(homeWindown, tray.getBounds(), alignment); positioner.position(homeWindown, tray.getBounds(), alignment);
isDev && homeWindown.webContents.openDevTools(); isDev && homeWindown.webContents.openDevTools();
// homeWindown.webContents.openDevTools(); homeWindown.webContents.openDevTools();
homeWindown.once('ready-to-show', () => { homeWindown.once('ready-to-show', () => {
homeWindown?.show(); homeWindown?.show();
}); });
...@@ -157,6 +158,7 @@ ipcMain.on('asynchronous-message', function (event, arg) { ...@@ -157,6 +158,7 @@ ipcMain.on('asynchronous-message', function (event, arg) {
}); });
ipcMain.on('window-close', (event) => { ipcMain.on('window-close', (event) => {
ipc.write(4, "Hello server")
app.quit(); app.quit();
}); });
...@@ -237,6 +239,7 @@ const createWindow = async () => { ...@@ -237,6 +239,7 @@ const createWindow = async () => {
{ {
label: '退出', label: '退出',
click: async () => { click: async () => {
ipc.write(4, "Hello server")
app.quit(); app.quit();
}, },
}, },
...@@ -246,7 +249,7 @@ const createWindow = async () => { ...@@ -246,7 +249,7 @@ const createWindow = async () => {
tray.setToolTip('北鲲云'); tray.setToolTip('北鲲云');
mainWindow.loadURL(resolveHtmlPath('index.html')); mainWindow.loadURL(resolveHtmlPath('index.html'));
// mainWindow.webContents.openDevTools(); mainWindow.webContents.openDevTools();
fs.mkdir(USER_HOME, { recursive: true }, (err: any) => { fs.mkdir(USER_HOME, { recursive: true }, (err: any) => {
if (err) return; if (err) return;
...@@ -269,7 +272,7 @@ const createWindow = async () => { ...@@ -269,7 +272,7 @@ const createWindow = async () => {
mainWindow.minimize(); mainWindow.minimize();
} else { } else {
mainWindow.hide(); mainWindow.hide();
// mainWindow.webContents.openDevTools(); mainWindow.webContents.openDevTools();
} }
}); });
...@@ -314,6 +317,7 @@ app.on('window-all-closed', () => { ...@@ -314,6 +317,7 @@ app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even // Respect the OSX convention of having the application in memory even
// after all windows have been closed // after all windows have been closed
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
ipc.write(4, "Hello server")
app.quit(); app.quit();
} }
}); });
......
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import { shell } from 'electron'; import { shell } from 'electron';
//js //js
...@@ -28,6 +28,8 @@ import siteIcon from '../../commons/assets/img/siteIcon.svg'; ...@@ -28,6 +28,8 @@ import siteIcon from '../../commons/assets/img/siteIcon.svg';
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
const electron = window.require('electron'); const electron = window.require('electron');
const ipc = require('../../commons/utils/ipc');
console.log('ipc: ', ipc);
const useStyles = makeStyles()((theme) => { const useStyles = makeStyles()((theme) => {
return { return {
...@@ -97,6 +99,8 @@ const list = [ ...@@ -97,6 +99,8 @@ const list = [
{ id: '5', value: siteIcon, lable: '设置' }, { id: '5', value: siteIcon, lable: '设置' },
]; ];
const ipc_one = ipc.getEvents()
export default (props) => { export default (props) => {
const { classes } = useStyles(); const { classes } = useStyles();
const { render, navigate, ipcSend } = public(); const { render, navigate, ipcSend } = public();
...@@ -105,12 +109,49 @@ export default (props) => { ...@@ -105,12 +109,49 @@ export default (props) => {
const [hover, setHover] = useState(false); const [hover, setHover] = useState(false);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
useEffect(()=>{
ipc.connect("cloudam", true)
},[])
var a = setInterval(myTimer, 50);
function myTimer() {
if (ipc.status() == "Closed") {
clearInterval(a)
clearInterval(b)
}
console.log('Connecton status: ' + ipc.status())
ipc.write(4, "Hello server")
}
ipc_one.on('connected', function (m) {
console.log("Client has connected")
});
ipc_one.on('data', function (m) {
console.log(m.type)
console.log(m.data.toString())
});
const iconClick = (e) => { const iconClick = (e) => {
console.log('e: ', e); console.log('e: ', e);
switch (e) { switch (e) {
case '1': case '1':
return; return;
case '2': case '2':
ipc.connect("cloudam", true)
return; return;
case '3': case '3':
ipcSend( ipcSend(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment