worker_processes 8;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream io_nodes {
ip_hash;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
server test.domain.local:81;
}
server {
listen *:3333; ## listen for ipv4
server_name test.domain.local;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_pass http://io_nodes;
}
}
}
var server = sticky(http.createServer(app));
но чет не дало никаких результатов(( const log = require("core/lib/log")(module);
require('globals');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
var http = require('http');
var redis = require('socket.io-redis');
if (cluster.isMaster) {
var workers = [];
var spawn = function(i) {
workers[i] = cluster.fork();
workers[i].on('exit', function(worker, code, signal) {
log.info(`respawning worker [${worker.process.pid}]`);
spawn(i);
});
};
for (var i = 0; i < numCPUs; i++) {
spawn(i);
}
}else {
var app = require('config/application'); //express
app.set('port', _G.cfg.port);
var server = http.createServer(app);
var io = require('socket')(server); //socket
io.adapter(redis({host: _G.cfg.rdb.host, port: _G.cfg.rdb.port}));
server.listen(_G.cfg.port,()=>{
app.set('io', io);
log.info(`Server listen port: "${server.address().port}"`);
});
server.on('error', (error) =>{
if (error.syscall !== 'listen') throw error;
switch (error.code) {
case 'EACCES':
console.error(`${_G.cfg.port} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${_G.cfg.port} is already in use`);
process.exit(1);
break;
default:
throw error;
}
});
}
init: function(cb){
this.io = io.connect();
this.io.on('connect', function () {
console.log('init socket');
this.initDefaultEvents();
cb();
});
}
module.exports = function(server){
var io = require('socket.io')(server);
io.set('origins',cfg.socket.origins);
io.clients = [];
io.use(function(socket, next) {
var handshakeData = socket.request;
handshakeData.cookies = cookie.parse(handshakeData.headers.cookie || ''); //get session id
var sidCookie = handshakeData.cookies[cfg.session.key];
var sid = cookieParser.signedCookie(sidCookie, cfg.session.secret);
if(!sid){
log.info('> Not session found');
next(new Error('Socket is not authorized'));
}
redis.get(`sess:${sid}`, (err, data) =>{
if(err){
log.error('Redis error: ',err);
next(new Error('not authorized'));
}
if(data) socket.handshake.user = $.jsonParse(data);
if(!socket.handshake.user) return next();
socket.handshake.user.sid = sid;
next();
});
});
io.sockets.on('connection', (socket) =>{
if(!socket.handshake.user) return false;
var socketID = socket.handshake.user.sid;
io.clients[socketID] = socket;
log.info("Socket is connect: [%s]",socketID);
socket.on('disconnect', () =>{
delete io.clients[socketID];
log.info("Socket is disconnect: [%s]",socketID);
});
});
return io;
};