2011-08-24 73 views
0

我使用opentok和socket.io包來嘗試創建2個「組」。我已經能夠將非分組用戶與1to1關係配對。我試圖做的是有兩組用戶。可以說一個組是一個服務檯,另一個組是一個客戶。我希望所有的客戶都可以組合在一起,但不能互相連接。我也希望與服務檯小組有同樣的行爲。然後我想讓任何1to1對組合在一起即ie。 helpdeskTOcustomer。我會提供一些代碼,但從邏輯的角度來看,我甚至不知道如何開始編碼,我將能夠提供的僅僅是稍微修改後的代碼。http://www.tokbox.com/developersblog/code-snippets/opentok-sessions-using-node-js/Socket.io對房間

回答

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文檔有點(我不熟悉它,看起來很整潔),看起來你只是想爲每種類型的用戶設置一個隊列,對吧?這裏有一些代碼(更像僞代碼,因爲我不熟悉你的應用或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); 
    } 
    } 
} 
+0

對不起,我很抱歉不夠清楚。我的意思是我希望對話具有1to1的比例。這意味着我想要將每個用戶歸類爲客戶或服務檯代表,如果每個組中至少有1個可用,我想將它們組合在一起。如果沒有來自另一組的可用人員配對,則用戶坐在隊列中,並且在一個人變爲可用時配對。這是否更有意義?這與chatroulette非常相似,除了聊天輪盤賭之外,任何兩個人在一起。這裏將是來自不同組的任何2人。 – DvideBy0

+0

啊,呃。看看OpenTok SDK的幫助。我添加了一些僞代碼;這些幫助有用? –

+0

是的,我認爲你讓我走上正軌。我真的在尋找一些東西,就像你爲了讓我去做一樣。我非常感激你花時間閱讀併爲我寫出一些幫助。我認爲opentok未來可以變成一項卓越的服務,許多小公司可以從可以讓客戶輕鬆與專業人士交流的方面受益。最好的部分是WebRTC變得可用並且您可以使用HTML5來製作視頻。想象一下,能夠在任何新型智能手機上做到這一點,不需要應用程序:) – DvideBy0