How to prevent more than 2 tokbox clients?

拥有回忆 提交于 2019-12-24 06:50:02

问题


I have the following code:

openTokInit() {
    this.session = OT.initSession(this.tokboxApiKey, this.sessionId);
    const self = this;
    this.session.on('connectionCreated', function(event) {
        self.connectionCount++;
    });

    if (this.connectionCount < 2) {
        this.session.connect(this.token, err => {
            if (err) {
                reject(err);
            } else {
                resolve(this.session);
            }
        });
    }

The problem is that when the if statement runs, the connectionCount is always 0, because the 'connectionCreated' event is fired a few seconds later. I'm not clear on how to appropriately wait for all the connectionCreated events to fire before connecting a new session.


回答1:


Adam here from the OpenTok team.

You won't get the "connectionCreated" Event until after you connect. So you will need to instead disconnect if you have connected and you are the 3rd (or more) participant. I would use the connection.creationTime to see who got there first to avoid 2 people connecting at about the same time and both of them disconnecting. Something like this should do the trick:

session = OT.initSession(apiKey, sessionId);
let connectionsBeforeUs = 0;
session.on('connectionCreated', (event) => {
  if (event.connection.connectionId !== session.connection.connectionId &&
     event.connection.creationTime < session.connection.creationTime) {
    // There is a new connection and they got here before us
    connectionsBeforeUs += 1;
    if (connectionsBeforeUs >= 2) {
      // We should leave there are 2 or more people already here before us
      alert('disconnecting this room is already full');
      session.disconnect();
    }
  }
});
session.connect(token);

Here is a jsbin that demonstrates it working.

I'm not sure how your whole application works but another option might be to do this at the server side and only hand out 2 tokens for people to connect. So when they try to get a 3rd token you block them at that point. Rather than letting them connect to the session and then disconnect themselves. The advantage of this approach is that you can notice quicker and give the user feedback sooner. Also a malicious user can't just hack the javascript and connect anyway. You could also use the session monitoring API to track users connecting from your server.

Another option again is to use the forceDisconnect() function to kick people out of a room if there are already 2 people in there. So it's the responsibility of the people already in the room to kick out the third participant rather than the 3rd participant noticing that there are already people in there and leaving themselves. This will mean that the malicious person couldn't hack the JavaScript code in their browser and join other people's rooms.

Without knowing your whole application though it's hard to know what the best option is for you.

I hope this helps!



来源:https://stackoverflow.com/questions/55081628/how-to-prevent-more-than-2-tokbox-clients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!