èµ·åããéã«ã
ã»expressã§ãµã¼ããç«ã¦ãï¼ãã¼ãã¯3000ï¼
ã»publicãã£ã¬ã¯ããªã®ä¸èº«ããã¹ãã£ã³ã°ãã
ã»socket.ioã§ãµã¼ãã¨ã¯ã©ã¤ã¢ã³ãã®éä¿¡ãè¡ãï¼ipcéä¿¡ã®ä»£æ¿ï¼
ã»Google Chromeãèµ·åãhttp://localhost:3000ãéãï¼Chromeã®ãã¦ã³ãã¼ãã¯å¥éå¿
è¦ï¼
ã¨ãããã¨ãè¡ãã¢ããªãã¤ããã¾ããã
å½ç¶ããã ãã§ã¯ä½ã®å½¹ã«ãç«ããªãã®ã§ãããExpress + socket.ioã§ä½ã£ãã¦ã§ããµã¤ããChromium以å¤ã®ãã©ã¦ã¶ã§è¦ãã¢ããªãä½ãããæã«éå½¢ã¨ãã¦ä½¿ãã¾ãã
Electronã¢ããªã«ãã¦ãã¾ãã¨ãChromiumã§ã¬ã³ããªã³ã°ããã¦ãã¾ãã®ã§ã
ç´è¿ã§Chromiumã§ã¯ãªãChromeã§ã¬ã³ããªã³ã°ãããã±ã¼ã¹ããã£ãã®ã§å¶ä½ãã次第ã§ãããã½ã¼ã¹ã³ã¼ããæ¸ãæããã°ä»ã®ãã©ã¦ã¶ãéããã¨ãå¯è½ã§ãã
å½ç¶ãPCã«Node.jsãå
¥ã£ã¦ããã°ãã¾ã£ãããã£ã¦ä¸å¿
è¦ãªã¢ããªãªã®ã§ãããNode.jsãã¤ã³ã¹ãã¼ã«ãããã¨ãªãexpressãµã¼ããç«ã¦ããã¨ãã§ããã¨ãããé
åã§ãã
Macã§ã®ã¿åä½ã確èªãã¦ã¾ãã
ã½ã¼ã¹ã³ã¼ã
package.json
{ "name": "electron-chrome", "version": "1.0.0", "main": "app.js", "scripts": { "start": "electron .", "build": "electron-packager . electron-chrome --platform=darwin --arch=x64" }, "repository": "[email protected]:kimizuka/electron-chrome.git", "author": "kimizuka <[email protected]>", "license": "MIT", "dependencies": { "express": "^4.17.1", "open": "^8.2.1", "socket.io": "^4.2.0" }, "devDependencies": { "electron": "^15.1.0", "electron-packager": "^15.4.0" } }
app.js
const { app } = require('electron'); const express = require('express'); const server = express(); const http = require('http').Server(server); const io = require('socket.io')(http); const open = require('open'); server.use('/', express.static(`${ __dirname }/public`)); io.on('connection', (socket) => { socket.on('disconnect', () => { app.quit(); }); }); http.listen(3000, '0.0.0.0'); open('http://localhost:3000', { app: open.apps.chrome });
public/index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>electron-chrome</title> <meta name="viewport" content="width=device-width, viewport-fit=cover, shrink-to-fit=no" /> <link rel="stylesheet" href="index.css" /> </head> <body> <h1>electron-chrome</h1> <script src="./socket.io.min.js"></script> <script type="module" src="index.js"></script> </body> </html>
public/index.js
'use strict'; const socket = io.connect(); socket.on('disconnect', () => { window.close(); });
é常ã«è¿·ã£ããã§ããã
ã»ã¢ããªãçµäºããéã«ããã©ã¦ã¶ãéãã
ã»ãã©ã¦ã¶ãéããéã«ãã¢ããªãçµäºãã
ãå®ç¾ããããã«ãapp.jsã¨public/index.jsã«ã
io.on('connection', (socket) => { socket.on('disconnect', () => { app.quit(); }); });
socket.on('disconnect', () => { window.close(); });
ã¨ããè¨è¿°ãå
¥ãã¦ãã¾ãã
app.jså´ã¯ãã©ã¦ã¶ããªãã¼ãããéãdisconnectãèµ°ããã¢ããªãçµäºãã¦ãã¾ããããéçºã®æã¯ã³ã¡ã³ãã¢ã¦ããã¦ãããæ¹ãè¯ãããã§ãã