node.js - 当服务器中的套接字数量超过 10 个时,是否难以保持与 Node 服务器的连接?

标签 node.js sockets socket.io

尝试与socket.io聊天 这是我的服务器

var io = require('socket.io');
connect = require('connect');
var Sequelize = require("sequelize");
var sequelize = new Sequelize('XXXXXX', 'XXXX', 'XXXXX');
var app = connect().use(connect.static('public')).listen(80,'myip');
var server_listen = io.listen(app);     
server_listen.sockets.on('connection',function(socket){         
            socket.on('entrance1',function(data){
                //insert socket id 
                if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
                    var friends_user='select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+data.user_id+' or friend_id= '+data.user_id+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+data.user_id+' or friend_id= '+data.user_id+') and is_active=1)';
                    sequelize.query('select * from msg_users_sockets where user_id='+data.user_id, null, {raw: true}).success(function(result){
                        if(result.length>0){
                            sequelize.query("INSERT INTO  `msg_users_sockets` (`user_id` ,`socket_id`,`cookie_id`)VALUES ('"+data.user_id+"',  '"+socket.id+"','"+result[0]['cookie_id']+"'); ").success(function(myTableRows) {});
                        }       
                        else{
                            var tim=new Date().getTime();
                            sequelize.query("INSERT INTO  `msg_users_sockets` (`user_id` ,`socket_id`,`cookie_id`)VALUES ('"+data.user_id+"',  '"+socket.id+"','"+tim+"'); ").success(function(myTableRows) {});
                        }   
                        sequelize.query(friends_user).success(function(result1){
                            socket.emit('online',{message:'you are online',user_id:data.user_id});
                            for(var i=0;i<result1.length;i++){
                                server_listen.sockets.socket(result1[i]['s']).emit('entrance',{message:'Anew chatter is online'+socket.id,user_id:data.user_id});
                            }                                           
                        }); 
                    });     
                }   
            });
            socket.on('remove_tab_user', function  (data) {
                if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
                    sequelize.query('select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where socket_id!="'+socket.id+'" and user_id='+data.user_id, null, {raw: true}).success(function(result){
                            for(var i=0;i<result.length;i++){
                            if(result[i]['s']!=socket.id)
                            server_listen.sockets.socket(result[i]['s']).emit('remove_tab_socket',data);
                        }           
                    });     
                }
            });
            socket.on('add_tab_user', function  (data) {
                if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
                    sequelize.query('select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where socket_id!="'+socket.id+'" and user_id='+data.user_id, null, {raw: true}).success(function(result){

                            for(var i=0;i<result.length;i++){
                                if(result[i]['s']!=socket.id)
                                server_listen.sockets.socket(result[i]['s']).emit('add_tab_socket',data);
                            }
                    });     
                }
            });         
            socket.on('chat', function  (data) {
            //  socket.broadcast.emit('updateusers',data);
                if(!isNaN(parseFloat(data[0].sender_id)) && isFinite(data[0].sender_id)){
                    var friends_user='select substring(socket_id, 1, length(socket_id)) s,socket_id i from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+data[0].sender_id+' or friend_id= '+data[0].sender_id+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+data[0].sender_id+' or friend_id= '+data[0].sender_id+') and is_active=1)';
                    sequelize.query(friends_user, null, {raw: true}).success(function(result1){
                            for(var i=0;i<result1.length;i++){
                                if(result1[i]['s']!=socket.id)
                                server_listen.sockets.socket(result1[i]['s']).emit('updateusers',data);
                            }                                           
                        });                         
                }
            });
            socket.on('disconnect', function  () {              
                sequelize.query('select * from msg_users_sockets where  socket_id='+socket.id, null, {raw: true}).success(function(result){
                        if(result.length>0){
                            sequelize.query('select * from msg_users_sockets where  user_id='+result[0]['user_id'], null, {raw: true}).success(function(result2){
                                if(result2.length>0){
                                    var friends_user='select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+result[0]['user_id']+' or friend_id= '+result[0]['user_id']+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+result[0]['user_id']+' or friend_id= '+result[0]['user_id']+') and is_active=1)';
                                    sequelize.query(friends_user).success(function(result1){
                                            for(var i=0;i<result1.length;i++){
                                                server_listen.sockets.socket(result1[i]['s']).emit('entrance2',{message:'Anew chatter is online'+socket.id,user_id:result[0]['user_id']});
                                            }   
                                        }); 
                                    }                                   
                            }); 
                        }
                sequelize.query("delete from msg_users_sockets where socket_id="+socket.id).success(function(myTableRows) {

                    });                 
                server_listen.sockets.emit('entrance',{message:'Anew chatter is disconnect'+socket.id});                
                });
            });

    });

这是我的客户端代码:

var socket=io.connect('http://myib:80',{
'max re connection attempts':5,
'sync disconnect on unload':true
 });

这是客户端事件 '错误' '重新连接失败' '连接失败' 以及一些聊天事件...等 当套接字尝试连接时给我这个警告: “websocket 连接无效” 一次或两次然后套接字连接 有时套接字会断开连接 当尝试重新连接套接字时给我同样的警告 当数字套接字 5 6 7 可能在某些情况下开始切断与其中一些套接字的联系并尝试再次连接,但会给出相同的警告并继续尝试,并且无法连接,直到再次重新加载页面

我正在使用最新版本的 Node 我尝试 io.set("transports", ["xhr-polling", "jsonp-polling"]); 请给我导致这个问题的所有可能性及其解决方案。 谢谢。

最佳答案

您没有启用可能导致问题的所有传输。使用这个:

io.set('transports', [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling'
'jsonp-polling',
  ]);
});

此外,我对您在中提到的服务器表示怀疑

var socket=io.connect('http://myib:80')

这是什么myib???你已经定义了吗?您应该将 localhost 或 IP 地址代替 myib

关于node.js - 当服务器中的套接字数量超过 10 个时,是否难以保持与 Node 服务器的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426642/

相关文章:

javascript - Electron Web Workers 不支持 NodeJS 模块

python - 通过套接字传递记录

c# - NetworkStream不支持查找操作

javascript - Socket.io 发送消息两次(或更多)

node.js - Socket.IO/node.js问题: unintended disconnects

node.js - Node :lts-alpine3. 14 和 Node :lts-alpine? 之间有什么区别,我应该将哪一个用于 Node 后端 api?

node.js - 在 google cloud sql nodejs 上定义 Sequelize

node.js - 使用 OR 条件序列化 FindOrCreate 函数

javascript - Socket.io node.js,如何记录连接时间避免或考虑页面刷新/多个套接字?

javascript - Socket.io:如何检查是否有一定数量的客户端发出了事件?