在express.io中连接客户端

时间:2014-02-10 17:09:09

标签: node.js express chat express.io

我正在使用node.js和express.io

开发一个简单的聊天应用

我想一直显示已连接客户端的列表(或在线聊天)。

express.io's doc中,一旦有新客户进入房间,就没有明确的方法来“获取”已连接客户的列表,即只有“广播”而不是“获取”。

以前有人这样做过吗?

任何线索都会非常有用。

谢谢!

编辑:

尝试@ jibsales的回答后。我想我们差不多了。客户回报我的不是真正的客户群,而是这一个:

[ { id: 'OWix3sqoFZAa20NLk304',
namespace: 
 { manager: [Object],
   name: '',
   sockets: [Object],
   auth: false,
   flags: [Object],
   _events: [Object] },
manager: 
 { server: [Object],
   namespaces: [Object],
   sockets: [Object],
   _events: [Object],
   settings: [Object],
   handshaken: [Object],
   connected: [Object],
   open: [Object],
   closed: [Object],
   rooms: [Object],
   roomClients: [Object],
   oldListeners: [Object],
   sequenceNumber: 496205112,
   router: [Object],
   middleware: [],
   route: [Function],
   use: [Function],
   broadcast: [Function],
   room: [Function],
   gc: [Object] },
disconnected: false,
ackPackets: 0,
acks: {},
flags: { endpoint: '', room: '' },
readable: true,
store: { store: [Object], id: 'OWix3sqoFZAa20NLk304', data: {} },
_events: 
 { error: [Function],
   ready: [Function],
   connection: [Function],
   NewChatPrivateLine: [Function],
   NewIdea: [Function],
   NewChatLine: [Function],
   NewPost: [Function] } } ]

功能是:

var app = require('express.io')();
app.io.route('connection', function(req) {
  req.io.join(req.data.room);
  var clients = app.io.sockets.clients(req.data.room);
  console.log(clients)
  app.io.room(req.data.room).broadcast('announce', {
    user: req.data.user,
    clients: clients
  }) 
});

这实际上返回一个错误(data = JSON.stringify(ev); TypeError:将循环结构转换为JSON),因为数组有几个圆形对象,因此无法广播。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

由于express.io只是粘贴expresssocket.io,因此请不要忘记查看socket.io的文档。话虽如此,自socket.io v0.7起,我们现在有了一个API方法来获取这些信息:

var clients = io.sockets.clients('room'); // all users from room `room`

不幸的是express.io是用coffeescript(UGGGH !!!)编写的,所以我很难读取源代码,但看起来好像需要express.io模块,socket.io实例也被提升了:

var express = require('express.io');
var clients = express.io.sockets.clients('room'); // all users from room `room`

如果这不起作用,我会抛弃express.io使用express和socket.io进行手动配置,因为看起来express.io有一个非常固执的API。它真的不是那么难,因为express.io只是创建一个漂亮的接口/抽象到手动配置(如果以上不起作用,这实际上会伤害你的用例)。

由于缺乏社区支持,我也会检查SockJS,因为我(以及许多其他websocket消费者)为SockJS抛弃了socket.io。更不用说,当回到xhr-polling时,IE9中存在严重的内存泄漏。

答案 1 :(得分:1)

好吧,最后我选择了@Brad提出的“解决方案”解决方案。它不是最优雅的,但如果你可以帮助我改进它,它会很棒!

这是最终代码:

服务器侧

var app = require('express.io')();

//To broadcast the users online in room sent by the client
var clients = [];
app.io.route('connect', function (req) {
  req.io.leave(req.data.room).on('disconnect', function() {
    //To remove client from list when disconnected
    var index = clients.indexOf(req.data.user);
    if (index > -1) {
      clients.splice(index, 1);
    }
    app.io.room(req.data.room).broadcast('announce', {
      user: req.data.user,
      clients: clients
    }) 
  });
  req.io.join(req.data.room);
  //To avoid repeating the same client with several opened windows/tabs
  var index = clients.indexOf(req.data.user);
  if (index === -1) {
    clients.push(req.data.user); 
  }
  app.io.room(req.data.room).broadcast('announce', {
    user: req.data.user,
    clients: clients
  }) 
});

<强>客户端

// Emit ready event with person name and predefined room for who's online
io.emit('connect', {
  room: room,
  user: user
});

//Get the signal from server and create your list
io.on('announce', function (data){
 //Do awesome stuff with data 
});