socket io - 向单个套接字发送消息

时间:2013-04-16 02:37:55

标签: jquery sockets socket.io emit

它最初是作为lynda.com教程项目

开始的

我尝试过并试过尝试让这个工作。我希望能够向特定聊天成员发送消息(将消息发送到一个套接字)。

我看着这个,我无法让我的工作。 Socket.IO - how do I get a list of connected sockets/clients?

我尽可能地切出这不是这个问题的一部分。

第一档:

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 :(得分: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});
}

希望这有助于将来。