这是我的服务器代码:
我正在尝试使用带有socket.io和expressjs的集群。我正在我的四核桌面上测试它。
var cluster = require('cluster')
var numCPUs = require('os').cpus().length
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork()
}
console.log(new Date());
}
else {
// get required modules
var connect = require('connect');
var express = require('express');
var mongo = require('mongodb');
var MongoStore = require('connect-mongo')(express);
var server;
var redis = require('redis');
var http = require('http');
// create server object
app = exports.module = express();
function configureServer() {
//configure environments
app.configure('production', function() {
app.use(express.errorHandler());
});
//configure server instance
app.configure(function() {
app.use(express.compress())
// set jade as default view engine
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser("refdgfd"));
app.use(express.methodOverride());
app.use(app.router);
// use express logger
});
loadModules();
}
var server = http.createServer(app).listen(80);
socket = require('socket.io').listen(server);
var RedisStore = require('socket.io/lib/stores/redis')
, pub = redis.createClient()
, sub = redis.createClient()
, client = redis.createClient();
socket.set('store', new RedisStore({
redisPub : pub
, redisSub : sub
, redisClient : client
}));
socket.configure(function() {
socket.set('log level', 1);
socket.set('transports', ['websocket']);
});
configureServer();
}
有什么突出的真正错误吗?
日志表明一切都发生了4次。这似乎是正确的。但我的socket.io的频道用户也表示每当我连接到一个页面时,我连接4次,(创建4个用户套接字)。这似乎是错的。我如何解决它,我的代码还有其他问题。感谢。