我们昨晚为我们的一个活动设置了一个实时代码,我们遇到了严重的性能问题。Socket.io 无法为连接到超过 400 到 500 个客户端的 socket.io.js 提供服务。代码很简单,每隔几分钟只有一条消息会广播给所有客户端,所以我认为代码没有太大的改进空间。服务器的硬件不是最好的,但我们在股票代码正常运行期间监控进程,但没有一个是导致问题的原因。
您是否知道如何解决问题,或者至少还有什么可能是导致问题的原因。看起来 socket.io 只是在挣扎,但不是因为缺乏硬件能力。
服务器结构
var io = require('socket.io').listen(443);
io.set('log level', 9);
//SQL CONNECTION
io.sockets.on('connection', function (socket) {
var sql_items = 'SELECT * FROM entries ORDER BY tstamp DESC';
db_query(sql_items , function(res_items) {
socket.emit('init', res_items);
});
socket.on('new_entry', function (data) {
//SECURE
if(!checkedSocketUsers[socket.id]) return false;
var currentTime = new Date();
if(currentTime.getMinutes() < 10);
var minutes = currentTime.getMinutes();
if(minutes < 10) minutes = "0" + minutes;
var hours = currentTime.getHours();
if(hours < 10) hours = "0" + hours;
var tstamp = currentTime.getTime() / 1000;
var time = hours + ":" + minutes;
sqli = "INSERT INTO entries (uid, tstamp, text, type) VALUES (null, "+tstamp+", '"+data.text+"', '"+data.type+"')";
client.query(sqli, function(err, info) {
var br_data = {};
br_data.time = time;
br_data.text = data.text;
br_data.uid = info.insertId;
br_data.type = data.type;
socket.broadcast.emit('broadcast_entry', br_data);
socket.emit('broadcast_entry', br_data);
});
});
socket.on('update_entry', function(data) {
//SECURE
if(!checkedSocketUsers[socket.id]) return false;
sqlu = "UPDATE entries SET text = '"+data.text+"' WHERE uid = "+data.uid;
client.query(sqlu, function(err, info) {
br_data = data;
socket.broadcast.emit('broadcast_update_entry', br_data);
});
});
socket.on('remove_entry', function(data) {
//SECURE
if(!checkedSocketUsers[socket.id]) return false;
var uid = data.uid;
sqld = "DELETE FROM entries WHERE uid = "+uid;
client.query(sqld, function(err, info) {
var br_data = {};
br_data.uid = uid;
socket.broadcast.emit('broadcast_remove_entry', br_data);
socket.emit('broadcast_remove_entry', br_data);
});
});
});
客户结构
socket = io.connect("http://localhost:443");
socket.on('init', function(data) {
//DOM Manipulation
});
socket.on('broadcast_entry', function(data) {
//DOM Manipulation
});
socket.on('broadcast_remove_entry', function(data) {
//DOM Manipulation
});
socket.on('broadcast_update_entry', function(data) {
//DOM Manipulation
});