0

It started off as a lynda.com tutorial project

I have tried and tried and tried to get this to work. I want to be able to send a message to a specific chat member (send a message to one socket).

I looked at this, and I was still unable to get mine to work. Socket.IO - how do I get a list of connected sockets/clients?

I've cut out as much as I could that was not part of this question.

First file:

var io = require('socket.io'), connect = require('connect'), chatter = require('./chatter.js');

var app = connect().use(connect.static('public')).listen(process.env.PORT || 3000);
var chatRoom = io.listen(app);

chatter.setSockets(chatRoom.sockets);

chatRoom.sockets.on("connection", function(socket){
 chatter.connectChatter({
  socket: socket,
  username: socket.id
 });
});

Second file:

     var allSockets = null;
        var members = [];

        exports.setSockets = function(sockets){
         allSockets = sockets;
        }

        exports.connectChatter = function(config){
         var exists = false;
         var self;
         for(var i = 0; i < members.length; i++){
          if(members[i].userName == config.username){
           exists = true;
           break;
          }
         }
         if(!exists){
          self = {userName:config.username};
          members.push(self);
         }

         config.socket.emit("welcome",{user:self});
         allSockets.emit("getUsers",{users:members});

        config.socket.on("chat", function(data){

    allSockets.emit("chat", {message:data.message}); //works just fine
             sockets[socket].emit("chat", {message:data.message}); //WILL NOT WORK
             sockets[0].emit("chat", {message:data.message}); //WILL NOT WORK
             socket[0].emit("chat", {message:data.message}); //WILL NOT WORK
             allSockets[socket].emit("chat", {message:data.message}); //WILL NOT WORK
             allSockets[0].emit("chat", {message:data.message}); //WILL NOT WORK

             allSockets.socket(0).emit('chat', {message:data.message}); //DOES NOTHING, BUT DOES NOT BREAK APP

Please help. I'm sure it's something stupid or silly. I'm pretty new to this. I just don't know what the syntax is.

Community
  • 1
  • 1
Shazboticus S Shazbot
  • 1,289
  • 2
  • 16
  • 27

1 Answers1

0

Okay got it working. Not sure why Lynda.com went with this coding layout and everyone else seemed to just do the io.sockets.socket style.

First file:

var io = require('socket.io'), connect = require('connect'), chatter = require('./chatter.js');

var app = connect().use(connect.static('public')).listen(process.env.PORT || 3000);
var chatRoom = io.listen(app);

chatter.setSockets(chatRoom.sockets);

chatRoom.sockets.on("connection", function(socket){
chatter.addSocket(socket);
 chatter.connectChatter({
  socket: socket,
  username: socket.id
 });
});

Second file:

var socketList = [];
exports.addSocket = function(s){
 socketList.push(s);
}

var allSockets = null;
        var members = [];

        exports.setSockets = function(sockets){
         allSockets = sockets;
        }

        exports.connectChatter = function(config){
         var exists = false;
         var self;
         for(var i = 0; i < members.length; i++){
          if(members[i].userName == config.username){
           exists = true;
           break;
          }
         }
         if(!exists){
          self = {userName:config.username};
          members.push(self);
         }

         config.socket.emit("welcome",{user:self});
         allSockets.emit("getUsers",{users:members});

        config.socket.on("chat", function(data){
        socketList[0].emit("chat", {message:data.message});
}

Hope this helps someone in the future.

Shazboticus S Shazbot
  • 1,289
  • 2
  • 16
  • 27
  • Just in case someone is wondering, this is not all the code. This is just the code necessary to get the socketList[0].emit function to work without breaking the app – Shazboticus S Shazbot Apr 16 '13 at 15:15