它最初是一个lynda.com教程项目

我已经试着尝试并尝试使它起作用。 我希望能够将消息发送到特定的聊天成员(将消息发送到一个套接字)。

我看着这个,但仍然无法让我工作。 Socket.IO-如何获取已连接套接字/客户端的列表?

我已经尽了最大努力来解决这个问题。

第一个文件:

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
 });
});

第二档:

     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

请帮忙。 我敢肯定这是愚蠢或愚蠢的。 我对此很陌生。 我只是不知道语法是什么。

===============>>#1 票数:0

好吧,它工作了。 不知道为什么Lynda.com会采用这种编码布局,而其他所有人似乎都采用io.sockets.socket样式。

第一个文件:

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
 });
});

第二档:

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});
}

希望这对以后的人有所帮助。

  ask by Shazboticus S Shazbot translate from so

未解决问题?本站智能推荐: