From 3e3539d50381de748ad9aead73c2a973894d3ae3 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 25 Aug 2018 20:54:35 +0800 Subject: [PATCH] change background to fork --- background.js | 12 +++++++++--- index.js | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/background.js b/background.js index 55ceb6d..cf0bf5b 100644 --- a/background.js +++ b/background.js @@ -308,9 +308,15 @@ const checkDonate = async (loginName = null, opayid = null) => { values }) let msg = `/me 感謝 ${i.name} 贊助了 ${i.amount} 元, ${i.msg}` - event.emit('twitchSend', { - msg, - channel: loginName + //event.emit('twitchSend', { + // msg, + // channel: loginName + //}) + process.send({ + type: 'twitchSend', + data: { + msg, channel: loginName + } }) } catch (err) { console.log(err) diff --git a/index.js b/index.js index 7e02b8d..3ddb90a 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,22 @@ require('module-alias/register') const fs = require('fs') +const fork = require('child_process').fork const path = require('path') try { fs.accessSync(path.resolve(__dirname, '.env')) require('dotenv').config() } catch (err) {} require('./app') -require('./background') -require('./event') +const event = require('./event') +let bgProc = fork('./background.js') +bgProc.on('message', data => { + if ('type' in data && 'data' in data) { + event.emit(data.type, data.data) + } +}) +bgProc.on('close', () => { + console.log(`background close`) +}) +//require('./background') const twitchChat = require('./libs/twitch-bot') twitchChat.runBot()