繁体   English   中英

Socket.io双人房

[英]Socket.io pair rooms

我使用opentok和socket.io包尝试创建2个“组”。 我已经能够将非分组用户与1对1关系配对。 我想做的是有2个用户组。 可以说一组是服务台,另一组是客户。 我希望将所有客户归为一组,但不要互相联系。 我也希望服务台小组也有同样的行为。 然后,我希望将任何一对1对1组合在一起,即。 帮助台TOcustomer。 我会提供一些代码,但是从逻辑的角度来看,我什至不知道如何开始对此进行编码,而我唯一能够提供的只是从这里进行了稍微修改的代码。http://www.tokbox.com/ developersblog / code-snippets / opentok-sessions-using-node-js /

从您的问题中不能确切地知道您正在尝试做什么(例如,“成对”或“分组在一起”是什么意思),但是您可能会发现Socket.IO的房间有一些用途。

有时您想在一个房间里放一堆插座,然后立即向他们发送消息。 您可以通过在套接字上调用join来利用房间,然后通过发送标志toin [发送数据或发出事件] in

 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文档之后(我并不熟悉它,看起来很整洁),看起来您只想为每种类型的用户排队,对吗? 以下是一些代码(更像是伪代码,因为我对您的应用程序或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);
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM