以下是我的服务器代码:
我正在尝试在我的四核桌面电脑上使用cluster、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个用户套接字),这似乎是错误的。我该如何解决它?代码中还有其他问题吗?谢谢。