From 55d469811bfe5457ffe30e0966232da186966388 Mon Sep 17 00:00:00 2001 From: Nur Muhammad Date: Sun, 13 Dec 2020 13:52:29 +0800 Subject: [PATCH] Remember multiple device sessions --- app-multiple-device.js | 65 ++++++++++++++++++++- index-multiple-device.html | 113 +++++++++++++++++++++++++++++++++++++ index.html | 65 ++++----------------- nodemon.json | 2 +- 4 files changed, 186 insertions(+), 59 deletions(-) create mode 100644 index-multiple-device.html diff --git a/app-multiple-device.js b/app-multiple-device.js index 5e062bf..d34ad71 100644 --- a/app-multiple-device.js +++ b/app-multiple-device.js @@ -18,13 +18,28 @@ app.use(express.urlencoded({ })); app.get('/', (req, res) => { - res.sendFile('index.html', { + res.sendFile('index-multiple-device.html', { root: __dirname }); }); const sessions = []; -const createSession = function(id, description, io) { +const SESSIONS_FILE = './whatsapp-sessions.json'; + +const setSessionsFile = function(sessions) { + fs.writeFile(SESSIONS_FILE, JSON.stringify(sessions), function(err) { + if (err) { + console.log(err); + } + }); +} + +const getSessionsFile = function() { + return JSON.parse(fs.readFileSync(SESSIONS_FILE)); +} + +const createSession = function(id, description) { + console.log('Creating session: ' + id); const SESSION_FILE_PATH = `./whatsapp-session-${id}.json`; let sessionCfg; if (fs.existsSync(SESSION_FILE_PATH)) { @@ -62,6 +77,11 @@ const createSession = function(id, description, io) { client.on('ready', () => { io.emit('ready', { id: id }); io.emit('message', { id: id, text: 'Whatsapp is ready!' }); + + const savedSessions = getSessionsFile(); + const sessionIndex = savedSessions.findIndex(sess => sess.id == id); + savedSessions[sessionIndex].ready = true; + setSessionsFile(savedSessions); }); client.on('authenticated', (session) => { @@ -87,6 +107,14 @@ const createSession = function(id, description, io) { }); client.destroy(); client.initialize(); + + // Menghapus pada file sessions + const savedSessions = getSessionsFile(); + const sessionIndex = savedSessions.findIndex(sess => sess.id == id); + savedSessions.splice(sessionIndex, 1); + setSessionsFile(savedSessions); + + io.emit('remove-session', id); }); // Tambahkan client ke sessions @@ -95,13 +123,44 @@ const createSession = function(id, description, io) { description: description, client: client }); + + // Menambahkan session ke file + const savedSessions = getSessionsFile(); + const sessionIndex = savedSessions.findIndex(sess => sess.id == id); + + if (sessionIndex == -1) { + savedSessions.push({ + id: id, + description: description, + ready: false, + }); + setSessionsFile(savedSessions); + } } +const init = function(socket) { + const savedSessions = getSessionsFile(); + + if (savedSessions.length > 0) { + if (socket) { + socket.emit('init', savedSessions); + } else { + savedSessions.forEach(sess => { + createSession(sess.id, sess.description); + }); + } + } +} + +init(); + // Socket IO io.on('connection', function(socket) { + init(socket); + socket.on('create-session', function(data) { console.log('Create session: ' + data.id); - createSession(data.id, data.description, io); + createSession(data.id, data.description); }); }); diff --git a/index-multiple-device.html b/index-multiple-device.html new file mode 100644 index 0000000..148948a --- /dev/null +++ b/index-multiple-device.html @@ -0,0 +1,113 @@ + + + + Whatsapp API by Ngekoding + + + + +
+

Whatsapp API

+

Powered by Ngekoding

+
+
+ +

+
+ +

+ +
+
+
+
+

+

+ QR Code +

Logs:

+
    +
    +
    +
    + + + + + + \ No newline at end of file diff --git a/index.html b/index.html index c2d1d88..4ab95ca 100644 --- a/index.html +++ b/index.html @@ -2,43 +2,15 @@ Whatsapp API by Ngekoding -

    Whatsapp API

    Powered by Ngekoding

    -
    -
    - -

    -
    - -

    - -
    -
    -
    -
    -

    -

    - QR Code -

    Logs:

    -
      -
      -
      + QR Code +

      Logs:

      +
      @@ -47,38 +19,21 @@ $(document).ready(function() { var socket = io(); - // Ketika button tambah diklik - $('.add-client-btn').click(function() { - var clientId = $('#client-id').val(); - var clientDescription = $('#client-description').val(); - var template = $('.client').first().clone() - .removeClass('hide') - .addClass(clientId); - template.find('.title').html(clientId); - template.find('.description').html(clientDescription); - $('.client-container').append(template); - - socket.emit('create-session', { - id: clientId, - description: clientDescription - }); + socket.on('message', function(msg) { + $('.logs').append($('
    • ').text(msg)); }); - socket.on('message', function(data) { - $(`.client.${data.id} .logs`).append($('
    • ').text(data.text)); - }); - - socket.on('qr', function(data) { - $(`.client.${data.id} #qrcode`).attr('src', data.src); - $(`.client.${data.id} #qrcode`).show(); + socket.on('qr', function(src) { + $('#qrcode').attr('src', src); + $('#qrcode').show(); }); socket.on('ready', function(data) { - $(`.client.${data.id} #qrcode`).hide(); + $('#qrcode').hide(); }); socket.on('authenticated', function(data) { - $(`.client.${data.id} #qrcode`).hide(); + $('#qrcode').hide(); }); }); diff --git a/nodemon.json b/nodemon.json index cf1c318..9624b4e 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,3 +1,3 @@ { - "ignore": ["whatsapp-session-*.json"] + "ignore": ["whatsapp-session-*.json", "whatsapp-sessions.json"] } \ No newline at end of file