Je partage mon Sketch, sur minimiser cacher sur l'icône de la barre des tâches, options du menu rigth-cliquez sur l'icône pour restaurer / fermer. en utilisant websocket / serveur http
//const {app, BrowserWindow} = require('electron');
const {app, BrowserWindow, Tray, Menu} = require('electron');
const myip = require('quick-local-ip');
const express = require('express');
const WebSocket = require('ws');
const bodyParser = require('body-parser');
const path = require('path')
// Config
const Config = {
http_port: '8080',
socket_port: '3030'
};
var iconpath = path.join(__dirname, 'rfid.png') // path of y
// Http server
const _app = express();
const server = require('http').Server(_app);
server.listen(Config.http_port);
// WSS server
const wss = new WebSocket.Server({port: Config.socket_port});
// Console print
console.log('[SERVER]: WebSocket on: ' + myip.getLocalIP4() + ':' + Config.socket_port); // print websocket ip address
console.log('[SERVER]: HTTP on: ' + myip.getLocalIP4() + ':' + Config.http_port); // print web server ip address
// 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;
let window;
let isQuiting;
let tray;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
acceptFirstMouse: true,
autoHideMenuBar: false,
useContentSize: true,
});
var appIcon = new Tray(iconpath);
// mainWindow.loadURL('index.html')
mainWindow.loadURL('http://localhost:8080');
mainWindow.focus();
// mainWindow.setFullScreen(true);
// Open the DevTools.
mainWindow.webContents.openDevTools();
var contextMenu = Menu.buildFromTemplate([
{
label: 'Show App', click: function () {
mainWindow.show()
}
},
{
label: 'Quit', click: function () {
app.isQuiting = true
app.quit()
}
}
])
appIcon.setContextMenu(contextMenu)
// Emitted when the window is closed.
mainWindow.on('close', function (event) {
mainWindow = null
});
mainWindow.on('minimize', function (event) {
event.preventDefault()
mainWindow.hide()
});
mainWindow.on('show', function () {
appIcon.setHighlightMode('always')
})
}
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X 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 OS X 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()
}
})
/**
* EXPRESS
*/
_app.use(bodyParser.urlencoded({
extended: false
}));
_app.use('/assets', express.static(__dirname + '/www/assets'))
_app.get('/', function (req, res) {
res.sendFile(__dirname + '/www/index.html');
});
/**
* WEBSOCKET
*/
wss.getUniqueID = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4();
};
wss.on('connection', function connection(ws, req) {
ws.id = wss.getUniqueID();
console.log('[SERVER]: Client Connected. ID=', ws.id);
ws.on('close', function close() {
console.log('[SERVER]: Client disconnected.');
});
ws.on('message', function incoming(recieveData) {
console.log('[SERVER] Message:', recieveData);
// Example use
// send(recieveData);
sendAll(recieveData);
});
// Send back to client
function send(data) {
data = JSON.stringify(data);
ws.send(data);
}
// Send to all clients
function sendAll(data) {
data = JSON.stringify(data);
wss.clients.forEach(function each(client) {
client.send(data);
});
}
});