0
Socket.io正在執行多個請求以執行握手並與客戶端建立連接。對於羣集,這些請求可能會到達不同的工作人員,這將破壞握手協議。 我嘗試使用radis,但在using multiple node servers with socket.io的socket.io文檔沒有成功nodejs中通過羣集的socket.io問題
var cluster = require('cluster');
var config = require('./config/environment');
if(cluster.isMaster) {
var server = require('http').createServer();
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client',
pingInterval: 25000,
pingTimeout: 30000
});
var redis = require('socket.io-redis');
socketio.adapter(redis({ host: '127.0.0.1', port: 6379 }));
var numWorkers = require('os').cpus().length;
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
} else {
startServer();
}
// Start server
function startServer() {
var express = require('express');
var mongoose = require('mongoose');
var multer = require('multer');
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
// Setup server
var app = express();
app.use(multer({ dest: './uploads/'}));
var server = require('http').createServer(app);
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client',
pingInterval: 25000,
pingTimeout: 30000
});
var redis = require('socket.io-redis');
socketio.adapter(redis({ host: '127.0.0.1', port: 6379 }));
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
server.listen(config.port, config.ip, function() {
});
// Expose app
exports = module.exports = app;
}