Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

javascript - Socket.io multiple connections

Strange issue I haven't really found documentation about. I think it may end up being a simple case of "you don't understand how the product works" and I'm hoping someone can fill the gap(s).

Here's what's going on... I have 3 separate apps which are socket.io servers. They're all listening on different ports. Each server is intended for a different specialized purpose. I'm building the application so that I can expand it in parts and only impact the individual isolated pieces I need to change/update.

This was working fine, until I realized that for each application running there's an extra socket connection per server. So if I have 3 apps, then I have 3 connections on each server.

The evidence of this is that if I add a console.log("Connected") to each server then connect a client, each server reports as many connections as there are servers. Hopefully this makes sense.

My goal, is I want 1 connection per server. It seems like the connections are each acting as a generic connection to all socket servers. My server listeners are set up like this :

io = require('socket.io').listen(26265) // can use up to 26485

My clients connect like this :

socket = new io('http://localhost:26265')

EDIT:

To add on to my original question so that you can see more code...

Full client code:

importJS('/js/pages/admin_base.js',function(){     
        AdminIO = new io('http://localhost:26266');
        AdminIO.on('send_users',function(rows){
            toggleLoad();
            /*
            if(typeof rows === 'object'){
                rows = Array(rows);
            }   
            */  
            appendUsers(rows);       
            console.log(rows);
        });
        AdminIO.on('failed_users',function(){
            toggleLoad();
            dropInfo("Failed to retrieve userlist",{level: "error"});
        });
        AdminIO.on('test',function (q) {
            console.log(q);
        });
    queryUsers(AdminIO);
});

The server code is pretty long... So the relevant pieces are :

var io = require('socket.io').listen(26266); // can use up to 26484
//.... imported additional modules and defined simple functions here
io.on('connection', function (socket) {  
    socket.on('restart_request', function(req){
        var success = false
            , session = JSON.parse(req.session)
            , sessionID = session.sessionID;
        checkSession(sessionID, function (ses) {
            if (ses === false) { console.error('CheckSession failed: No session exists'); return; }
            if (ses.user.uuid !== session.uuid) { console.error('CheckSession failed: UUID mismatched'); return; }
            if (ses.user.role < conf['Permissions']['lm_restart']){ socket.emit('restart_fail','Insufficient permissions.'); return; }
            if(process.platform === 'win32'){            
                executeCMD('START "" .\windows\scripts\restart_lm.bat',function(err,res){
                    var errSent = false;
                    if(err){                        
                        console.error(err);
                        if(!errSent){ socket.emit('restart_fail','Restart failed'); }
                        errSent = true;
                        if(res === null){return;}
                    }
                    console.log(res);
                    socket.emit('restart_success','LM successfully restarted.');
                });                
            }
            else if(process.platform === 'linux'){

            }
        });
    });
    socket.on('get_users',function(req){
          var success = false
            , session = JSON.parse(req.session)
            , opts = req.opts || null
            , sessionID = session.sessionID
            , col = opts.col || null
            , where = opts.where || null
            , range = opts.range || null
            ;
        checkSession(sessionID, function (ses) {
            if (!ses) { console.error('CheckSession failed: No session exists'); return; }
            if (ses.user.uuid !== session.uuid) { console.error('CheckSession failed: UUID mismatched'); return; }
            if (ses.user.role < conf['Permissions']['lm_user_query']){ socket.emit('userQuery_fail','Insufficient permissions.'); return; }            
            Query.users({col: col, where: where, range: range},function(err,res){
                if(!err){socket.emit('send_users',res);}
                else {socket.emit('failed_users',true);} 
            });            
        });
    });
    socket.on('test',function(q){
        socket.emit('test',q);
    });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try removing the 'new' keyword from the io thing. You shouldn't use 'new' there since it would make new instances every time you reload the page or a new client connects. So, it should look like: Server side:

var io = require('socket.io')(26265);

Client side:

var socket = io('http://localhost:26265');

I think this is what you were looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...