#OpenTok how enumerate streams in a session?

耗尽温柔 提交于 2019-12-11 05:07:11

问题


I'm subscribing to only one of many publishers, one at a time (video monitor). So, I initially open all subscribers and when the onconnected event is fired, I store each of them in a hash table (stream.connection.data holds ID). I subscribe to the first, then unsubscribe and subscribe to the next. However, I've had to kill the onconnected event handler so they didn't keep firing multiple times: Tokbox streamCreated being called same number of times client is called

Now, when a new participant joins the session late (after the event handler has been removed) I don't know how to subscribe to their stream since no event fires. I know they have connected and are streaming because I get a message from their application that contains the ID of their stream.

So, I need a way to iterate through all the steams in the session, find the new one, and subscribe to it when it's that person's turn. How, can I get all the streams in a session and look at their connection ID's?


回答1:


TokBox Developer Evangelist here.

To know the number of streams in a session on the client side, I recommend listening to the following session events:

  • streamCreated - fires when someone starts publishing in a session
  • streamDestroyed - fires when someone stops publishing in a session.

I would create an object like below to keep a record of all streams in a session:

const streams = {};
session.on({
  streamCreated: event => {
    streams[event.stream.streamId] = event.stream;
  },
  streamDestroyed: event => {
    delete streams[event.stream.streamId];
  },
};

This would then allow you to access the stream object and subscribe like so:

const stream = streams['f39c6-ae02-100c-9727-b3bf2']; // please note that this is a random stream Id
const subscriber = session.subscribe(stream);

If you would like to know the number of streams in a session on the server side, you can use Session Monitoring and listen to the same events.



来源:https://stackoverflow.com/questions/53947066/opentok-how-enumerate-streams-in-a-session

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