gpt4 book ai didi

node.js - nodejs socket.io 除了连接 100% cpu 之外什么都不做

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:30 25 4
gpt4 key购买 nike

我是nodeJs和socket.io的新手,当我运行它时,当我的站点中有大约800个用户时,它的CPU利用率达到100%(CPU在14-103%之间跳跃..)。起初,我看到的问题是,每隔几分钟,所有用户都会断开连接,并在几秒钟后重新连接,因此我注释掉了所有代码,最后都以 on connect 和 on disconnect 结尾。5分钟后CPU仍然达到跳跃CPU 14-100%..我不知道要检查什么,因为除了最基本的东西之外什么都没有。 Node 版本 - 0.10.25socket.io 版本 - 1.0代码片段 -

var redis = require('redis').createClient(),
server = require('http').createServer(),
request = require('request'),
io = require('socket.io').listen(server),
sockets = {};
// , memcache = require('memcache')
// , cookie = require('cookie')

// run HTTP server on this port
server.listen(3000);

console.log('started node sockets.');

// only allow authenticated connections - TODO: check if its doing anything.. :x
io.set('authorization', function(handshake, callback)
{
return callback(null, true);

});

io.sockets.on('connection', function(socket)
{
var id = getUserId(socket);
if( id === false) return;
// store user's socket
sockets[id] = socket;
console.log('User ' + id + ' connected');
request({url:"http://www.-----.co/nodeConnect", qs:{id: id}},function(){})


socket.on('disconnect', function()
{
id = getUserId(socket);
if( id === false ) return;
// store user's socket
delete sockets[id];
console.log('User ' + id + ' disconnect');
request({url:"http://www.----.co/nodeDisconnect", qs:{id: id}},function(){});

});

function getUserId(socket) {
if( typeof socket.handshake.headers.cookie == 'undefined' ) return false; // HOTFIX - check it
var cookie = socket.handshake.headers.cookie;
var startAt = cookie.indexOf('; uid=') + 6;
var tempUid = cookie.substr(startAt);
return tempUid.substr(0, tempUid.indexOf(';') ); // return id.
}

服务器是一个非常强大的服务器,32cpu,120gb ram。运行 nginx、php、nodejs,大约 1000 人..我应该检查一下让它运行而不用完我所有的CPU吗?顺便说一句,这个 Node 应用程序实际应该使用多少 CPU?

最佳答案

问题是连接和断开连接中的请求,它们很慢,并且由于大量流量,CPU 被占用太多。

关于node.js - nodejs socket.io 除了连接 100% cpu 之外什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com