{"size": 2462, "ext": "js", "max_stars_repo_path": "ws-server.js", "max_stars_repo_name": "posiyans/amg", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "ws-server.js", "max_issues_repo_name": "posiyans/amg", "max_issues_repo_issues_event_min_datetime": "2021-03-09T12:06:50.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T15:21:34.000Z", "max_forks_repo_path": "ws-server.js", "max_forks_repo_name": "posiyans/amg", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 28.2988505747, "max_line_length": 85, "alphanum_fraction": 0.5349309504}
const url = 'amg.loc';
var request = require('request'),
    io = require('socket.io')(6001, {
        origins: url+':*'
    }),
    Redis = require('ioredis'),
    redis = new Redis();
var users = {};
var firstUser = true;

// проверяем авторизованный поьзватель или нет
// проверяем первый пользователь, если да то всех оффлайн
io.use(function (socket, next) {
    let option = {}
    if (firstUser){
        option = {
            firstUser: true,
        };
    }
    request.get({
        url: 'http://' + url + '/ws/check-auth',
        headers: {cookie: socket.request.headers.cookie},
        qs: option,
        json: true
    }, function (error, response, json) {
        socket.user_id = json.user_id
        users[socket.user_id] = socket.id;
        if (json.auth){
            firstUser = false;
        }
        return json.auth ? next() : next(new Error('Auth error'));
    });

});

io.on('connection', function (socket) {
    io
        .emit('userList', users);
    socket.on('actionUser', function (channel, data) {
        //console.log('actionUser ' + channel)
        io
            .to(channel + ':actionUser')
            .emit(channel + ':actionUser', {user_id : socket.user_id, chat_id:data});
    });
    // проверка доступа к  каналу
    socket.on('subscribe', function (channel) {
        //console.log(io)
        request.get({
            url: 'http://' + url + '/ws/check-sub/' + channel,
            headers: {cookie: socket.request.headers.cookie},
            json: true
        }, function (error, response, json) {
            if (json.access) {
                socket.join(channel);
                socket.join(channel + ':actionUser');
            } else {
                return;
            }
        });
    });
    // устанавливаем статус офлайн при отключении
    socket.once('disconnect', function () {
        delete users[socket.user_id];
        io
            .emit('userList', users);
        request.get({
            url: 'http://' + url + '/ws/status-offline/',
            headers: {cookie: socket.request.headers.cookie},
            json: true
        });
        //console.log(users);
    })
});

redis.psubscribe('laravel_database*', function (error, count) {

});

redis.on('pmessage', function (pattern, channel, message) {
    message = JSON.parse(message);
    console.log(message);
    io
        .to(channel + ':' + message.event)
        .emit(channel + ':' + message.event, message.data);
});

