Socket.io pair rooms
Im using the opentok and socket.io packages to try and create 2 "groups". I have been able to pair up non grouped users with a 1to1 relationship just fine. What Im trying to do is have 2 groups of users. Lets say one group is a help desk and the other group is a customer. I want all the customers to be grouped together but not connect to one another. I would also like for the same behavior with the help desk group. I then want any 1to1 pair to group together ie. helpdeskTOcustomer. I would provide some code, but from a logical standpoint im not even sure how to begin coding this and the only thing I would be able to provide is just the slightly modified code from here.. http://www.tokbox.com/developersblog/code-snippets/opentok-sessions-us开发者_开发知识库ing-node-js/
It's not really clear from your question exactly what you're trying to do (e.g. what do you mean by "pair" or "group together"), but you may find some use of Socket.IO's rooms.
Sometimes you want to put a bunch of sockets in one room, and send a message to them [all at once]. You can leverage rooms by calling
join
on a socket, and then [send data or emit events] with the flagsto
andin
: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'); });
[Edit]
Okay, after seeing your comment and looking over the OpenTok docs a bit (I wasn't familiar with it, it seems pretty neat), it looks like you just want a queue for each type of user, right? Here's a bit of code (more like pseudocode, as I'm not intimately familiar with your app or the 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);
}
}
}
精彩评论