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.