const { nativeImage, Menu } = require('electron') const { app, BrowserWindow, Notification, ipcMain, Tray } = require('electron/main') const path = require('node:path') const socketio = require('socket.io-client') let win = null let getlock = app.requestSingleInstanceLock() if(!getlock){ app.quit() } else { if (win) { if (win.isMinimized()) win.restore() win.focus() } } const createWindow = () => { if (!tray){ createTray() } let win = new BrowserWindow({ width: 1280, height: 720, autoHideMenuBar: true, icon: path.join(__dirname, 'assets/images/icon.jpg'), title: "Treehouse Hub", webPreferences: { preload: path.join(__dirname, 'preload.js'), nodeIntegration: false } }) win.loadURL('http://184.83.153.182:5000/') win.on('closed', function () { win = null }) } let tray = null function createTray(){ let icon = path.join(__dirname, 'assets/images/icon.jpg') const trayicon = nativeImage.createFromPath(icon) tray = new Tray(trayicon.resize({width:16})) const contextMenu = Menu.buildFromTemplate([ { label: 'Open Window', click: () => { createWindow() } }, { label: 'Quit', click: () => { app.quit() } } ]) tray.setContextMenu(contextMenu) } function showNotification (title, message) { const icon = path.join(__dirname, 'assets/images/icon.jpg') new Notification({title:title, body:message, icon:icon }).show() } app.whenReady().then(() => { createWindow() app.setAppUserModelId("Treehouse Hub") app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) const socket = socketio('ws://184.83.153.182:5000/') socket.emit('join-notifications') socket.on('connect-notifications', () => { console.log('connected to notification stream') showNotification('Websocket Connected', "You successfully connected to the websocket.") }) socket.on('send-notification', (data) => { showNotification(data.title, data.body) }) }) app.on('window-all-closed', (event) => { if (process.platform !== 'darwin') { event.preventDefault() } }) // Listen for messages from the renderer process ipcMain.on('notification', (event, title, message) => { console.log('notifiy') showNotification(title, message) });