0

i'm making a chat application divided by several namespaces, in other words i want to divide interests, some of them like to talk 'dogs' other wants to talk about 'cats' and so on...

This is my first version, in which i stored each namespace in a variable(it works perfectly) :

Server side:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);


app.get('/default', function(req, res){
  res.sendfile('index1.html');
});

app.get('/dog', function(req, res){
  res.sendfile('index2.html');
});

app.get('/', function(req, res){
  res.sendfile('index.html');
});



var cns1 =  io.of('/default');
//default nsp
cns1.on('connection', function(socket){

  socket.on('chat message', function(msg){
    cns1.emit('chat message', msg);
  });

});


var cns2 =  io.of('/dog');

//dog nsp
cns2.on('connection', function(socket){

  socket.on('chat message', function(msg){
    cns2.emit('chat message', msg);
  });

});

var cnsindex =  io.of('/');

//index nsp
cnsindex.on('connection', function(socket){

  socket.on('chat message', function(msg){
    cnsindex.emit('chat message', msg);
  });

});


http.listen(3000,function(){
  console.log('listening on *:3000');
});

index*.html

    <script>
          //on index.html
          var socket = io.connect('http://localhost:3000/');
          //on index2.html
          //var socket = io.connect('http://localhost:3000/dog');
          //on index1.html
          //var socket = io.connect('http://localhost:3000/default');

$(function(){     

$('#bb').click(function (){

            socket.emit('chat message', $('#m').val());
            $('#m').val('');
            return false;
});

});

socket.on('chat message', function(msg){
    $('#messages').append($('<li>').text(msg));
  });




        </script>

Each namespaces keeps their messages private.

Now, when i wanted to store all Workspaces in an array to avoid repeating events, like that:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);



app.get('/default', function(req, res){
  res.sendfile('index1.html');
});

app.get('/dog', function(req, res){
  res.sendfile('index2.html');
});

app.get('/', function(req, res){
  res.sendfile('index.html');
});




var nss = [
    io.of('/default'),
    io.of('/dog'),
    io.of('/')
];


for (i in nss) 
{
    nss[i].on('connection', function(socket){

            socket.on('chat message', function(msg){
              nss[i].emit('chat message', msg);
            });

        });  
}


http.listen(3000,function(){
  console.log('listening on *:3000');
});

In the second version doesn't receive messages for /dog and /default urls, and it allow sending messages from /dog and /default to /.

I'm stuck here, helps please!

Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80
  • 2
    See here - http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue – levi Jul 18 '14 at 19:03

1 Answers1

0

Solved, this is a problem of closure thx to @levi :

for (i in namespaces) 
{
    namespaces[i].on('connection',handleConnection(namespaces[i]));  

    function handleConnection(ns)
    {
        return function (socket){
                    socket.on('chat message', function(msg){
                      ns.emit('chat message', msg);
                    });
                }
    }


}

Now my code works :)

Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80