我目前正在使用node.js和socket.io开发拍卖脚本.But网站是使用PHP& amp; MySQL的。这里我只使用node.js + socket.io进行拍卖竞标过程。在拍卖期间,该网站将有500-1000名登录用户查看单个页面。只有一件商品将在拍卖中出售,并且会在一天内出售。
我将向服务器到客户端的所有用户广播(发出)倒数计时器。在服务器端,我将使用setInterval(),1秒的递归setTimeout()倒计时到拍卖结束时间。除此之外,发送的唯一其他消息将是当前的出价从单个客户端传递到服务器然后广播到所有。这样做可靠吗?它能够处理服务器上的使用吗?在这里,我已经测试了500个用户意味着在浏览器中挂起计时器。
var cluster = require('cluster');
var app = require('express')();
//var http = require('http');
var https = require('https');
var socket = require('socket.io');
var redis = require('redis');
var redisAdapter = require('socket.io-redis');
var request = require('request');
var fs = require('fs');
var options = {
key: fs.readFileSync('keys/e1317_0f2c9_71565598d419e37e376ccef5c2827113.key'),
cert: fs.readFileSync('certs/e1317_0f2c9_1468152279_2dc46c1f2cc135a.crt'),
ca: fs.readFileSync('cabundles/90490a5c829d2aca24f22b5820864c6e_1935558000.cabundle')
};
//var server = http.createServer( app );
var server = https.createServer(options, app);
var io = socket.listen(server);
var port = process.env.PORT || 8080;
var workers = process.env.WORKERS || require('os').cpus().length;
var redisUrl = process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379';
var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
var pub = redis.createClient(redisOptions.port, redisOptions.host, {
detect_buffers: true,
return_buffers: true,
auth_pass: redisOptions.password
});
var sub = redis.createClient(redisOptions.port, redisOptions.host, {
detect_buffers: true,
return_buffers: true,
auth_pass: redisOptions.password
});
io.adapter(redisAdapter({
pubClient: pub,
subClient: sub
}));
console.log('Redis adapter started with url: ' + redisUrl);
io.sockets.on('connection', function(client) {
//console.log('first');
client.on('nauction', function(data) {
io.sockets.emit('nauction', data);
});
});
io.on('connection', function(socket) {
//console.log('in');
console.log('connected client count:' + io.sockets.sockets.length);
var recursive = function() {
//console.log("It has been one second!");
if (io.sockets.sockets.length > 0) {
request('https://www.example.com/file.php', function(error, response, body) {
if (!error && response.statusCode == 200) {
data = JSON.parse(body);
socket.volatile.emit('auction_data', {
'auction_data': data
});
//console.log(data);
} else {
//console.log('else');
console.log(error);
}
});
} //else{
//console.log('No clients connected now');
//}
setTimeout(recursive, 1000);
}
recursive();
socket.on("disconnect", function() {
console.log('clear interval')
//clearInterval(interval);
clearTimeout(recursive);
});
});
if (cluster.isMaster) {
console.log('start cluster with %s workers', workers - 1);
workers--;
for (var i = 0; i < workers; ++i) {
var worker = cluster.fork();
console.log('worker %s started.', worker.process.pid);
}
cluster.on('death', function(worker) {
console.log('worker %s died. restart...', worker.process.pid);
});
} else {
start();
}
function start() {
server.listen(port, function() {
console.log('listening on *:' + port);
});
}
socket.on('auction_data', function(auction_details) {
//console.log(auction_details);
$.each(auction_details, function(keys, values) {
//countdwon formation
var tm, days, hrs, mins, secs;
days = value.auction_data.time.days;
if (value.auction_data.time.hours < 10) {
hrs = ("0" + value.auction_data.time.hours);
} else {
hrs = value.auction_data.time.hours;
}
if (value.auction_data.time.mins < 10) {
mins = ("0" + value.auction_data.time.mins);
} else {
mins = value.auction_data.time.mins;
}
if (value.auction_data.time.secs < 10) {
secs = ("0" + value.auction_data.time.secs);
} else {
secs = value.auction_data.time.secs;
}
if (days == 0) {
tm = '' + hrs + '' + '' + mins + '' + '' + secs + '';
} else {
tm = '' + days + '' + '' + hrs + '' + '' + mins + '' + '' + secs + '';
}
$('#auction_' + value.auction_data.product_id + " .countdown").html(tm);
});
});
我正在等待您解决浏览器挂起问题的答案。
答案 0 :(得分:4)
第一个问题: 这样做是否可靠?
不需要每隔一秒向每个客户发送时间。只需在第一次访问时向他们发送时间并使用本地计时器(在他们的本地页面)减少每秒的时间。
您还需要检查每次出价的服务器时间(更安全)。
如果这对您来说不够“安全”,请使用更改的出价发送时间。您只需发送实际出价,更改时(使用广播)或用户加入网站(只需将其发送给他)。
第二个问题: 它是否能够处理服务器上的使用情况?
是和否。
如果您的Sever足够好(每5美元服务器拥有无限流量), 你不应该遇到麻烦。只有,如果您的脚本非常糟糕并且内存泄漏播种。
现在提供一些提示:
如果这是您所希望的答案,请选择左侧的绿色箭头。 如果没有,请在此处写评论,我会提供更多提示。