'use strict';

const cluster = require('cluster');
const path = require('path');


module.exports = function(server) {
    const socket_io = require('socket.io')(server);
    socket_io.on('connection', (socket) => {
        socket.emit('stalk-data', 'Connecting...');
        socket.emit('connection', 'Done!');

        socket.on('stalk-request', user_id => {
            cluster.setupMaster({
                exec: path.resolve(__dirname, '../vkstalk'),
                args: ['stalk', user_id],
                silent: true
            });

            let worker = cluster.fork();
            worker.process.stdout.on('data', buffer => {
                socket.emit('stalk-data', buffer.toString('utf8'));
            });
            socket.emit('stalk-data', user_id);
        });
    });
};

@@ -164,16 +164,41 @@
 erver);%0A
+    const workers = %7B%7D;%0A%0A
     sock
@@ -603,16 +603,58 @@
 fork();%0A
+            workers%5Buser_id%5D = worker.id;%0A
         
@@ -698,24 +698,24 @@
 buffer =%3E %7B%0A
-
             
@@ -786,24 +786,33 @@
     %7D);%0A    
+    %7D);%0A%0A
         sock
@@ -818,34 +818,114 @@
 ket.
-emit('stalk-data', 
+on('disconnect', () =%3E %7B%0A            Object.keys(cluster.workers).forEach(id =%3E cl
 us
+t
 er
-_id
+.workers%5Bid%5D.kill()
 );%0A 

Kill all cluster.workers on socket client disconnect.