Socket.io配对房间

时间:2011-08-24 01:39:21

标签: javascript node.js socket.io

我使用opentok和socket.io包来尝试创建2个“组”。我已经能够将非分组用户与1to1关系配对就好了。我想做的是有两组用户。可以说一组是帮助台,另一组是客户。我希望所有客户组合在一起但不能彼此连接。我也希望与帮助台组有相同的行为。然后,我希望任何1对1组合在一起,即。 helpdeskTOcustomer。我会提供一些代码,但从逻辑的角度来看,我甚至不确定如何开始编码,我唯一可以提供的只是稍微修改过的代码.. http://www.tokbox.com/developersblog/code-snippets/opentok-sessions-using-node-js/

1 个答案:

答案 0 :(得分:3)

从你的问题确切地你想要做什么(例如你的意思是“对”或“组合在一起”)并不是很清楚,但你可能会发现一些使用Socket的东西.IO的房间

  

有时候你想把一堆插座放在一个房间里,并向他们发送信息[一次全部]。您可以通过调用套接字上的join来利用会议室,然后使用标记toin [发送数据或发出事件]来利用会议室:

io.sockets.on('connection', function (socket) {
  socket.join('a room');
  socket.broadcast.to('a room').send('im here');
  io.sockets.in('some other room').emit('hi');
});

[编辑]

好的,看到你的评论并稍微查看了OpenTok文档(我不熟悉它,看起来非常整洁),看起来你只想为每种类型的用户建一个队列,对吧?这里有一些代码(更像是伪代码,因为我对你的app或OpenTok API并不熟悉):

var customers_waiting = [];
var employees_waiting = [];

io.sockets.on("connection", function(socket) {
  // Determining whether a connecting socket is a customer
  // or an employee will be a function of your specific application.
  // Determining this in this callback may not work depending on your needs!
  if(/* client is a customer*/) {
    customers_waiting.push(socket); // put the customer in the queue
  else if(/* client is an employee */) {
    employees_waiting.push(socket); // put the employee in the queue
  }

  try_to_make_pair();
});

function try_to_make_pair() {
  if(customers_waiting.length > 0 && employees_waiting.length > 0) {
    // If we have people in both queues, remove the customer and employee
    // from the front of the queues and put them in a session together.
    customer = customers_waiting.shift();
    employee = employees_waiting.shift();

    opentok.createSession('localhost', {}, function(session) {
      enterSession(session, customer);
      enterSession(session, employee);
    }
  }
}